Global variable being ambiguous?

Sep 8, 2013 at 8:31pm
Hello, thanks for viewing my question.

I am teaching myself C++ from a book; the program below is supposed to show an example of local and global variables and how they are used.

As I was about to run and compile my program I ran into an error: "count" is ambiguous. I use the global variable more than once in the program so here is one example: count = i* 2;.

Now I have been researching on the internet about this compile error and some said its because of the following code using namespace std; and I know this tells the compiler to use all the std namespace, but I have tired to just remove that line of code and then the cout statements turn up an error; then I tired to use the code std::cout but I have no idea what they are used for, as my book just says use using namespace std; for now.

From what I found on the internet using Google the line of code using namespace std; has something to do with the count variable or something along the lines of that?

Here is the whole program:
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
#include <iostream>
using namespace std;


void func1();
void func2();

int count;

int main() {
	int i;

	for(i=0; i<10; i++) {
		count = i* 2;
		func1();
	}
	cout << "\n\n";

	return 0;
}

void func1() {
	cout << "Count: " << count;
	cout << "\n";
	func2();
}

void func2() {
	int count;

	for(count=0; count<3; count++) cout << ".";
}


Thanks if you answer and help me out!

Luke...
Sep 8, 2013 at 8:38pm
Under the <algorithm> header, there is the std::count function, which is probably what you read about.

Your issue is from lines 8 and 29. You declared two variables with the same name. Because the count from line 8 is global, it can be seen by func2(), so it was if you had:
1
2
int a;
int a; //Error 


Edit:
My compiler just gives me a warning that the variable on line 29 "shadows" the global variable.
Last edited on Sep 8, 2013 at 8:40pm
Sep 8, 2013 at 8:38pm
It runs fine for me. What compiler are you using?

When you refer to the global variable count, try ::count instead. Example:

1
2
3
4
5
void func1() {
	cout << "Count: " << ::count;
	cout << "\n";
	func2();
}
Sep 8, 2013 at 8:47pm
Yeah I read about the <algorithm> header. I see your point @Daleth but the program is suppose to show a global variable from line 8 and a local variable from line 29; is that right?

I am using MS visual studio 2012 ultimate @Danny Toledo also I used the ::count; and the worked! But why do I have to use that as my book doesn't tell me to; my book is wrote by Herbert Schildt and I've been told he is known for errors maybe that's why.

Thanks!
Last edited on Sep 8, 2013 at 8:48pm
Sep 8, 2013 at 8:54pm
There is something called the global namespace.
With the std namespace, you see variables and functions scoped with std::. But the global namespace is nameless, which is why you use ::count instead.
It is a way of specifying that you want the count from the global and not local scope, like you can call ::func1() or ::operator+(1.3f, 2.0f).
Last edited on Sep 8, 2013 at 8:54pm
Sep 8, 2013 at 8:58pm
Arrh thank you, I understand it now; I thought that was what it was used for the ::count, as I'm a beginner I wasn't sure :)

Thanks again!
Topic archived. No new replies allowed.