Elzza 0 Posted September 12, 2014 Share Posted September 12, 2014 I wrote a function that uses a global variable, but this variable isn't working properly. A function that reads / records the number to this variable, not always doing it properly, why? test.h int global_var[3]; // Global Variable class test { public: void write(int nmb); int read(); }; extern test testt; test.cpp #include "test.h" ... void test::write(int nmb) { global_var[0] = nmb; } int test::read() { return global_var[0]; } questlua_pc.cpp ... #include "test.h" ... test testt; int pc_test_func_write(lua_State* L) { testt.write(lua_tonumber(L,1)); return 1; } int pc_test_func_read(lua_State* L) { lua_pushnumber(L, testt.read()); return 1; } ... {"test_func_write", pc_test_func_write }, {"test_func_read", pc_test_func_read }, This is a sample code. How can I fix it? Link to comment Share on other sites More sharing options...
Honorable Member martysama0134 7177 Posted September 12, 2014 Honorable Member Share Posted September 12, 2014 smth.h extern int smth[3];smth.cpp int smth[3] = {1,2,3};I don't like such awful thing: extern test testt; Just, implement the constructor inside smth.cpp and also give a DEFAULT value to smth[3]; (or maybe include the array inside the class) You're not giving information about what kind of error you got and what are you trying to do with such "implementation". Check out my GitHub Link to comment Share on other sites More sharing options...
Elzza 0 Posted September 12, 2014 Author Share Posted September 12, 2014 Just, implement the constructor inside smth.cpp and also give a DEFAULT value to smth[3]; (or maybe include the array inside the class) In my full code, I've done it earlier, but error is still. edited code: test.h class test { public: int global_var[3]; // Global Variable test(); void write(int nmb); int read(); }; extern test testt; test.cpp #include "test.h" ... test::test() { global_var[0] = 0; global_var[1] = 0; global_var[2] = 0; } void test::write(int nmb) { global_var[0] = nmb; } int test::read() { return global_var[0]; } smth.h extern int smth[3]; smth.cpp int smth[3] = {1,2,3}; I don't like such awful thing: extern test testt; I did it because I want to use only one class object in different functions. How can I do it differently? Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now