-
September 13th, 2005, 09:29 PM
#1
Global Variables
How will I declare a global variable in VC++? I have created a struct on one of my class (lets say class1) after the #include.... It was included in the Global folder in the class view tab but when I'm going to access it on a different class (class2), the error is "undeclared identifier". I've tried to put the struct on the header file of class1 but a linker error occured. How will i be able made a struct be accessible by all of my classes? Thanks, I'm really stuck
-
September 13th, 2005, 09:52 PM
#2
Re: Global Variables
Without looking at your code, it is quite difficult to understand your problem. Anyway, since you have already declared the struct class1, you still have to create an instance. Therefore in your class1 header file, you have to declare the global instance of class1 as extern. After that, you have to include class1 header file in class2 cpp file.
Code:
// In class1.cpp file
class1 myobject; // Declaring a global instance.
// In class1.h file
struct class1
{
//...
};
extern class1 myobject; // Allows other cpp file that include this header file to have access to myobject.
// In class2.cpp file
#include "class1.h"
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
September 14th, 2005, 08:41 PM
#3
Re: Global Variables
Oh, ok, I got it. I have to put the struct{}; inside the header file then declare the extern on the .cpp file. Thanx a lot
-
September 1st, 2012, 04:44 PM
#4
Re: Global Variables
Originally Posted by Kheun
Without looking at your code, it is quite difficult to understand your problem. Anyway, since you have already declared the struct class1, you still have to create an instance. Therefore in your class1 header file, you have to declare the global instance of class1 as extern. After that, you have to include class1 header file in class2 cpp file.
Code:
// In class1.cpp file
class1 myobject; // Declaring a global instance.
// In class1.h file
struct class1
{
//...
};
extern class1 myobject; // Allows other cpp file that include this header file to have access to myobject.
// In class2.cpp file
#include "class1.h"
Reviving an old thread but it's applicable to my question:
Assume class2.cpp contains:
Code:
// class2.cpp
#include "class1.h"
#include "class3.h"
#include "class4.h"
void test ()
{
myobject.variable1 = 5;
}
When myobject is used in class2:
1. How does one know whether "myobject" was instantiated in class1.cpp or class3.cpp or class4.cpp? Must one just hunt for the instantiation in all of the files?
2. What will happen if one inadvertently instantiated "myobject" twice? Like once in class1.cpp and again in class3.cpp.
Thanks,
Raptor (who's writing his first C++ program)
Last edited by raptor88; September 1st, 2012 at 04:46 PM.
-
September 1st, 2012, 08:24 PM
#5
Re: Global Variables
When myobject is used in class2:
1. How does one know whether "myobject" was instantiated in class1.cpp or class3.cpp or class4.cpp? Must one just hunt for the instantiation in all of the files?
2. What will happen if one inadvertently instantiated "myobject" twice? Like once in class1.cpp and again in class3.cpp.
Found the answer for question-2.
..... Got an "already defined in ......" error when compiled.
Any answers for question-1?
-
September 1st, 2012, 10:37 PM
#6
Re: Global Variables
Originally Posted by raptor88
1. How does one know whether "myobject" was instantiated in class1.cpp or class3.cpp or class4.cpp? Must one just hunt for the instantiation in all of the files?
Source files are just text files. You have to search for whatever definition you're lookiing for.
Regards,
Paul McKenzie
-
September 1st, 2012, 10:42 PM
#7
Re: Global Variables
Originally Posted by raptor88
2. What will happen if one inadvertently instantiated "myobject" twice? Like once in class1.cpp and again in class3.cpp.
If class1.cpp and class3.cpp are separate compilable modules, no error will occur at compile time.
Code:
// class1.cpp
class SomeClass
{ };
SomeClass myobject;
Code:
// class2.cpp
class SomeClass
{ };
SomeClass myobject;
When compiled separately, no error. So how are you getting two C++ source files to know what's in the other source file, unless you're erroneously including one CPP source file in another CPP source file?
The error will come when you attempt to link. The linker will see the multiple definitions and give you a linker error (not a compiler error).
Regards,
Paul McKenzie
-
September 2nd, 2012, 05:41 AM
#8
Re: Global Variables
Originally Posted by raptor88
1. How does one know whether "myobject" was instantiated in class1.cpp or class3.cpp or class4.cpp? Must one just hunt for the instantiation in all of the files?
Originally Posted by Paul McKenzie
Source files are just text files. You have to search for whatever definition you're lookiing for.
Figured that there's no way to know other than searching for the definition in every file.
Thanks for the confirmation.
Raptor
-
September 2nd, 2012, 05:45 AM
#9
Re: Global Variables
Originally Posted by Paul McKenzie
When compiled separately, no error. So how are you getting two C++ source files to know what's in the other source file, unless you're erroneously including one CPP source file in another CPP source file?
The error will come when you attempt to link. The linker will see the multiple definitions and give you a linker error (not a compiler error).
A noob thing on my part. Should have said "compiled and linked". IOW, what happens when the green arrowhead to start debugging is clicked.
Thanks,
Raptor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|