0

First, the code snippets I'm having issues with;

settings.h

   namespace
{
int WINDOW_WIDTH;
int WINDOW_HEIGHT;
bool FULLSCREEN;
}

void settings_init();

settings.cpp

#include "DX_Constants.h"

void settings_init()
{
 WINDOW_WIDTH = 1920;
 WINDOW_HEIGHT = 1080;
 FULLSCREEN = true;

}

main.cpp //just the important part..i haven't forgotton the other pieces needed by winapi

#include "settings.h"

int WINAPI WinMain(...)
{
settings_init();

if(//verifies values entered are seen here correctly ...NOTE triggers on != )
return false;


....code continues
}

my problems:

  1. running the code in this format triggers the if statement check and closes the program.

  2. if i remove the namespace anonymous i get LNK 2005 errors for the variables.

  3. I CAN get the code to run if i simply comment out ALL of settings.cpp and put the function definition into the header file, but this is a shortfix to me.

what I need: obviously more understanding about header and secondary .cpp files...and probably linking. I have a couple books; "programming 2d games" by kelly, "beginning game programming" by harbour, and "visual C++ 2010" by horton. can someone point me in the direction of a place to learn in more detail my problem, which is getting the functions to live in .cpp while the declarations live in the header, or recommend a book that will teach me to understand these issues?

Solution edit: Credit goes to Mats Petersson (see the comments)

solution for the code ended up being;

  1. remove the anonymous namespace and brackets.
  2. apply extern prefix in front of the global variable type declarations in settings.h .
  3. ADD normal global variable declaration ( ex. int window_width; ) to the settings.cpp file.
Okari
  • 5
  • 1
  • 3

1 Answers1

0

The names WINDOW_HEIGHT and WINDOW_WIDTH are likely used as macros or other names in the Windows environment. Using variables that are all upper-case names is generally a bad idea, as this is the convention for macros, so the chances of accidentally conflicting with some macro defined in some header-file that you didn't even know was being included is much greater.

I would suggest that you change the names of your variables so that they don't conflict - the simple way to do that is to use lower-case names, window_height and window_width, etc.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • I tried what you suggested, same errors and issues, the lnk 2005 error that i mentioned in 2. is more specifficially error LNK2005: "int window_width" (?window_width@@3HA) already defined in settings.obj – Okari Apr 14 '13 at 02:36
  • That sounds like you are compiling and linking the same source twice, which I think is because you should be having `extern` in front of the name, and then a second declaration of the same names in `settings.cpp` (without `extern`). Otherwise, you do get one copy of the variables for every source file that includes the header, which isn't what you want. – Mats Petersson Apr 14 '13 at 08:41
  • And of course, the fact that you have two copies is also why your code goes wrong, since `main` is reading the "wrong copy". I'll come back and edit the answer in a bit, but I'm off to have some breakfast now... – Mats Petersson Apr 14 '13 at 08:43
  • That did it! Thank you. I'm used to working with class declarations and prototyping in the header and then the functions existing in the .cpp. I did not realize that global variables acted different. If I want to learn more about this "extern" prefix any hints as to where I should begin looking? – Okari Apr 14 '13 at 22:52