Question

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.
Was it helpful?

Solution

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.

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