Trouble with global variables

Jun 11, 2008 at 6:46pm
Hi there,

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

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 << '.';
}



I'm using wxDev C++ with the default options

Hope someone can help, I'd love to get past this elementry stage and get on to something more meaty.
Jun 11, 2008 at 6:47pm
by the way, I did use indent's but they seem to have got lost when I posted
Jun 11, 2008 at 6:48pm
You need to use the [c0de] your code [/c0de] tags to display code. Replace the 0 with o.

extern int count = 0;

Try that :)
Jun 11, 2008 at 9:20pm
Thanks, Ok, tried that..

This time I got first a warning :

 
9 D:\C Projects\CppABeginersGuide\globalvariable.cpp [Warning] `count' initialized and declared `extern' 

(I know this isn't really code but I just wanted to try it)

Then.. the same two errors that I encountered before.

I'm wondering if it is something that is not set up quite right in wxdev c++
Jun 11, 2008 at 9:28pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

void func1();
void func2();

using namespace 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 << '.';
}
Jun 11, 2008 at 9:46pm
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 using namespace 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.
Jun 11, 2008 at 9:47pm
:: says that you want to use the global variable.
Jun 13, 2008 at 3:58pm
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()
{
	using namespace 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()
{
	using namespace std;
	
	Count++;
	
	cout << Count << endl;
	
	return 0;
}


Compiles without problems.

Is it that count is some sort of reserved word, can someone please explain this?
Last edited on Jun 13, 2008 at 4:03pm
Jun 13, 2008 at 8:09pm
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.
Jun 15, 2008 at 8:16pm
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.
Topic archived. No new replies allowed.