Jump to content

[src]Global variable not work


Recommended Posts

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

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

  • Honorable Member

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".

Link to comment
Share on other sites

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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.