views:

700

answers:

4

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?

Here is a direct link: http://i44.tinypic.com/103gcbk.jpg

A: 

But it doesn't seem to be showing up when I use the img tag after posting?

Here is a direct link: http://i44.tinypic.com/103gcbk.jpg

Please remove this - I've edited the material into the question.
Jonathan Leffler
+3  A: 

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
}
erikkallen
Thanks, this was indeed a stupid mistake of mine :)
Minor point, but it doesn't shadow a global, because there is no global - just a reference to one that doesn't exist.
Paul Beckingham
+1  A: 

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

jblocksom
You can do it that way, but it isn't particularly clean.
Jonathan Leffler
+3  A: 

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 ()
{
  ...
}
Paul Beckingham