Unable to define a global variable in C++

后端 未结 2 1807
南方客
南方客 2020-12-22 12:14

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

相关标签:
2条回答
  • 2020-12-22 12:39

    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.

    0 讨论(0)
  • 2020-12-22 12:48

    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/

    0 讨论(0)
提交回复
热议问题