Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

where's the connection? 1

Status
Not open for further replies.

mover50

Programmer
Aug 3, 2004
77
US
When I do compile of my C++ program, I get this error, basically telling me that count has been declared elsewhere.
Where is the connection to the 'stl_algo.h' where it was also declared?

I am running it on linux FC2.

g++ prog3-02.cpp -o prog3-02
prog3-02.cpp: In function `int main()':
prog3-02.cpp:20: error: use of `count' is ambiguous
prog3-02.cpp:11: error: first declared as `int count' here
/usr/include/c++/3.3.3/bits/stl_algo.h:390: error: also declared as `typename
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here
prog3-02.cpp: In function `void func1()':
prog3-02.cpp:28: error: use of `count' is ambiguous
prog3-02.cpp:11: error: first declared as `int count' here
/usr/include/c++/3.3.3/bits/stl_algo.h:390: error: also declared as `typename
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here



Here is my program. If I change the references to count to something like countt, it works ok. According to my documentation, count is not a reserved word.
Here is the version.

RPM version 4.3.1
Copyright (C) 1998-2002 - Red Hat, Inc.
This program may be freely redistributed under the terms of the GNU GPL

My program.

/* Program #3-02 Global Variables

*/

#include <iostream>
using namespace std;

void func1();
void func2();

int count; // this a global variable

int main()

{
int i; // this is a local variable

for(i=0; i<10; i++)
{
count = i * 2;
func1();
}
return 0;
}

void func1()
{
cout << "count: " << count; // access global count
cout << '\n'; // newline
func2();

}

void func2()

{
int count; //local variable for this function only.

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



Kent (the worrier)
 
Please use [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

> Where is the connection to the 'stl_algo.h' where it was also declared?
That's because you did
Code:
using namespace std;
EVERY symbol which is prefixed with std::name can now be referred to as name.
So the std::count in that header file now matches your count, and thus you get the "ambiguous" error.

Since you only need cout, you should be more specific and just ask for the bits which interest you
Code:
using std::cout;

Or even skip that and just write in the code
Code:
std::cout << "count: " << count;

> If I change the references to count to something like countt, it works ok.
Which is why namespaces exist in the first place, to protect one persons use of "count" from another persons use of "count".
Just typing in "using namespace std;" blows all that away and you're back to the "good old days" of conflicting symbols and choosing your own prefix to minimise the possibility of conflicts.


On a different topic, try this
Code:
g++ -W -Wall -Wshadow hello.cpp

--
 
Or add the using to scopes that use cout, and then anywhere count is used using the scoping resolution operator instead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top