Global variable prob
-
You can put your definition of your structure in any header file. To create a global variable for your struct, you can declare it in a source file. When you need to access this in another file, declare it again but with the
extern
keyword. E.g.// File1.h
struct MY_STRUCT {
...};// GlobalVariable.cpp
#include "File1.h"MY_STRUCT myStruct;
// AnotherFile.cpp
#include "File1.h"extern MY_STRUCT myStruct;
Weiye Chen Life is hard, yet we are made of flesh...
-
Global variables are almost always a bad idea. You're better off creating a class with a static public variable. The main reason is that you have some ability to control/track access of the variable that way. The easiest way to add a global is to declare it in your stdafx.cpp. You may need to declare it in stdafx.h, but I'm pretty sure from memory that stdafx.cpp is the place to make it visible across your app. Christian Graus - Microsoft MVP - C++
-
If you were going to have a global as a static property with get and set methods in a class, then yes, you'd define the struct in the same header file, before you defined the class. Or define it in the CPP and forward declare it, if you choose to store it as a pointer. If you put it in stdafx, then you define it in stdafx.h, I would have thought. Christian Graus - Microsoft MVP - C++
-
If you were going to have a global as a static property with get and set methods in a class, then yes, you'd define the struct in the same header file, before you defined the class. Or define it in the CPP and forward declare it, if you choose to store it as a pointer. If you put it in stdafx, then you define it in stdafx.h, I would have thought. Christian Graus - Microsoft MVP - C++
-
Well, you can do it either way, but doing it in a class is definately better, and no harder. Christian Graus - Microsoft MVP - C++
-
If you were going to have a global as a static property with get and set methods in a class, then yes, you'd define the struct in the same header file, before you defined the class. Or define it in the CPP and forward declare it, if you choose to store it as a pointer. If you put it in stdafx, then you define it in stdafx.h, I would have thought. Christian Graus - Microsoft MVP - C++
-
I've placed the struct on stdafx.cpp then the extern part on stdafx.h. But 'undeclared identifier' error still occurs
-
oh.. undeclared identifier is no longer the error. The error now is at the stdafx.h. I guess I've placed it in the wrong area. Where am I exactly allowed to place the externs? Sorry for the disturbance
benjnp wrote: The error now is at the stdafx.h. What error? benjnp wrote: Where am I exactly allowed to place the externs? After those includes Weiye Chen Life is hard, yet we are made of flesh...
-
Hello... benjnp wrote: I thought redeclaring the struct will reset all its values or perhaps reinitialize them. No, only without the extern keyword... With the extern keyword you can declare global variables over many files without to reset them... They will be all associated to the first declaration... :)
-
benjnp wrote: The error now is at the stdafx.h. What error? benjnp wrote: Where am I exactly allowed to place the externs? After those includes Weiye Chen Life is hard, yet we are made of flesh...