c++ - pointer to array from serperate .cpp -


trying point array separate .cpp getting null values array.

main.cpp

int main() {     storage exp;     cout << exp.pointer[0];     _getch();     return 0; } 

storage.h

class storage {     public:     storage();     int* pointer = experience;     int storage::experience[10];     ~storage(); }; 

storage.cpp

storage::storage() { } int experience[10] = { 100, 200, 400, 600, 1000, 2500, 3000, 4000, 5000, 10000; storage::~storage() { } 

it's rpg thingy. need return array values can't , can't create array scratch because it's handmade values. has go somewhere. don't want put in main(done before in separate code) because i'm trying learn how pointers doing terribly wrong.

i don't think storing pointers , having each object contain own best way this. if objects of storage class share same values experience[10] array, should declare static int, not int, in declaration of storage class in storage.h. don't specify it's inside class storage; you're declaring variable inside class declaration, isn't needed.

class storage {     //other variables     static int experience[10]; }; 

then in storage.cpp, put:

int storage::experience[10] = { 100, 200, 400, 600, 1000, 2500, 3000, 4000, 5000, 10000 }; 

(don't forget end brace before semicolon!)

then objects in storage class "share" int array; there's 1 instance of member objects can access read.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -