1

I'm new to programming and have been happily working my way through C++ A Beginner's Guide (which I'm thoroughly enjoying!). However, I've come across a bit of an issue. In chapter 5, Schildt talks about global variables and he presents this small program to show how they could be used:

#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();
    }
    cin.get();
    return 0;
}

void func1()
{
    cout << "count: " << count; // Access global count
    cout << "\n";
    func2();
}

void func2(){
    int count;
    for (count = 0; count < 3; count++)
        cout << ".";
}

When I compile the code, I'm presented with an error message whenever the variable count is used within the main block and other functions of the program. Is this an issue with the compiler (Visual Studio Express 2013? Do I need to prefix the global variable with something so that it can be used?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 6
    1) What is the error message? 2) Don't say `using namespace std`, especially if you are going to use names that belong to the `std` namespace for something else. – juanchopanza Apr 18 '14 at 16:58
  • It will complain because you have `count` in both local and global scope. i.e. warning you to make sure that you are not using the wrong one. Therefore it is a good idea to avoid global variables in first place – Ed Heal Apr 18 '14 at 16:59
  • I don't see any compile errors: http://ideone.com/VJ9I9g – Vaughn Cato Apr 18 '14 at 16:59
  • can you post the error message appeared in the console? – Shang Apr 18 '14 at 17:00
  • Hi. The error message is: errorC2872:'count'ambiguous symbol. – user3329662 Apr 18 '14 at 17:00
  • @user3329662 - Refer to the comment above – Ed Heal Apr 18 '14 at 17:02
  • @user3329662 What statement is erroneous? – Vlad from Moscow Apr 18 '14 at 17:03
  • I believe the reason why it give you an error is because VSE2013 doesn't want user to define multiple type of same name (error displayed as ambiguous) If you change the name of global count (that means count in main and func1() or the count inside the func2() it should be alright. You can run standard g++ compiler it will give you result without errors. – Shang Apr 18 '14 at 17:07
  • Solved! Thanks for the speedy reply everyone. Juanchopanza - you were correct. I changed all 'count' variable names to 'numbers' instead and the program compiled. I wonder why the example showed you to use a keyword as a variable. Odd but the first error that I've come across. Thanks all! – user3329662 Apr 18 '14 at 17:07
  • 3
    You need to **burn** the Schildt book. E.g., when some highly competent people (including me) maintain that SO has devolved too far into becoming "Herb Schildt-land", that refers to his books. Schildt has a nack for writing books that beginners enjoy, and that at first sight seem plausible, but almost all explanations are invented, bollocks. It's said that the low price of his C++ annotated standard reference reflected the value of his commentary. Other than that, *thank your favorite deity* that you're unable to define global variables, because they're mostly just Evil. – Cheers and hth. - Alf Apr 18 '14 at 17:09
  • 1
    Hmm I would be suspicious of any book that set that code as an example. BTW the stuff may compile on some platforms because `std::count` lives in the `algorithm` header. So it looks like in this case it is being indirectly included via `iostream`. That kind of stuff can happen. – juanchopanza Apr 18 '14 at 17:11

2 Answers2

8

count is defined in both your code and by the Standard Library (in the std namespace). Your use of using namespace std; to drag the entire Standard namespace into the global namespace creates an ambiguity. You should do at least one of the following:

  • remove using namespace std from the global namespace; either use the namespace within your functions, or use just the names you need, or qualify all the standard names when you use them carefully choose your own names to avoid conflicts with standard names
  • change the name count to *something else to avoid ambiguity.
  • qualify the references to the global count, writing ::count.


*) Note in particular that the standard library also defines the name distance.
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
Qasim
  • 5,181
  • 4
  • 30
  • 51
5

I'm guessing this is close to the error you get:

In function 'int main()':
Line 13: error: reference to 'count' is ambiguous
compilation terminated due to -Wfatal-errors.

Using the namespace std makes count refer to std::count which is an algorithm in the standard library.

http://www.cplusplus.com/reference/algorithm/count/

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Martin G
  • 17,357
  • 9
  • 82
  • 98