I'm new to C++ and I'm trying to teach myself using the book "C++ a beginners guide" by Herbert Schildt
My problem is that on page 183 of this book is an example of how to use global variables but I can not get it to work. Everything I have read up to now has worked fine so I'm sure I've done something wrong, athough having read this through several times I can't see where I've made a mistake.
The errors I'm getting are:
D:\C Projects\CppABeginersGuide\globalvariable.cpp In function `int main()':
16 D:\C Projects\CppABeginersGuide\globalvariable.cpp `count' undeclared (first use this function) (Each undeclared identifier is reported only once for each function it appears in.)
D:\C Projects\CppABeginersGuide\globalvariable.cpp In function `void func1()':
26 D:\C Projects\CppABeginersGuide\globalvariable.cpp `count' undeclared (first use this function)
Basically it seems to be saying that I have not declared my variable 'count', but I have!! right before the main() function.
Here's the code:
#include <iostream>
#include <cstdlib>
using namespace std;
void func1();
void func2();
int count; // This is the global variable that the compiler says is undeclared
#include <iostream>
void func1();
void func2();
usingnamespace std;
int count;
int main() {
int i; // This is a local variable
for(i=0; i<10; i++) {
::count = i *2;
func1();
}
system ("PAUSE");
return 0;
}
void func1()
{
cout << "count: " << ::count; // access global count
cout << "\n";
func2();
}
void func2()
{
int count; // This is a local variable
for(count=0; count<3; count++) cout << '.';
}
Wow! that worked thanks!! That was giving me a real headache!
How did putting two colons before the variable name fix it? Is it something to do with the usingnamespace std; statement? Like maybe, count is already defined in the std namespace as doing something else important and therefore the compiler won't let you use it.
If so, I wonder why the editors didn't notice when the book was published (it's a second edition too) unless it's just a wxdev c++ thing and I'm just barking comletely up the wrong tree.
I understand what you said Zaita, but I still don't get why, by just changing the name of the variable count to something like Count, makes it work whit out making use of ::?
This:
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
int count = 0;
int main()
{
usingnamespace std;
count++;
cout << count << endl;
return 0;
}
Doesn't compile with the error:
$ c++ question.cpp -o question
question.cpp: In function ‘int main()’:
question.cpp:8: error: ‘count’ was not declared in this scope
Whereas this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
int Count = 0;
int main()
{
usingnamespace std;
Count++;
cout << Count << endl;
return 0;
}
This is a bit strange, neither count or Count are reserved although the error implies a different problem. It appears to expect count to be declared within the main code block, like this....
1 2 3 4 5 6
int main()
{
int count = 0;
...
return 0;
}
The fact that it differs between upper and lowercase C suggests there is another variable called count defined somewhere, probably in a library. By using the :: you are explcitly telling the compiler to use the global one defined in the file.
With your original code. The problem lies in that you have a class with the same variable name declared in that file. This leads to possible ambiguity so the compiler appears to take the full-safe approach and make you specify which scope you want when calling that variable.
It's not really a good idea to have global and class variables with the same name.