Question

I drew a little graph in paint that explains my problem:

But it doesn't seem to show up when I use the <img> tag after posting?

Graph:

http://i44.tinypic.com/103gcbk.jpg

Was it helpful?

Solution

You need to instantiate the database outside of main(), otherwise you will just declare a local variable shadowing the global one.

GameServer.cpp:

#include GameSocket.h
Database db(1, 2, 3);
int main() {
   //whatever
}

OTHER TIPS

The problem is the scope of the declaration of db. The code:

extern Database db;

really means "db is declared globally somewhere, just not here". The code then does not go ahead and actually declare it globally, but locally inside main(), which is not visible outside of main(). The code should look like this, in order to solve your linkage problem:

file1.c

Database db;
int main ()
{
  ...
}

file2.c

extern Database db;
void some_function ()
{
  ...
}

The extern is being applied to all the CPP (and resulting OBJ) files, so none of them ever actually instantiate the DB.

Here's one way around this. In Database.h, change the extern Database db to:

#ifdef INSTANTIATE_DB
Database db;
#else
extern Database db;
#endif

and then in one of your CPP files (Database.cpp would be good, if you have one) add a #define INSTANTIATE_DB before the #include "Database.h".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top