Reputation: 101
I'm getting a compilation error saying 'left' and 'right' are ambiguous.
Did I declare left, right at wrong place?
How would I fix this?
Minimum test case:
#include <iostream>
using namespace std;
int left = 0, right = 0;
int main()
{
cout << left;
cout << right;
}
is giving:
prog.cpp: In function ‘int main()’:
prog.cpp:6:13: error: reference to ‘left’ is ambiguous
prog.cpp:3:5: error: candidates are: int left
In file included from /usr/include/c++/4.7/ios:43:0,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from prog.cpp:1:
/usr/include/c++/4.7/bits/ios_base.h:918:3: error:
std::ios_base& std::left(std::ios_base&)
prog.cpp:7:13: error: reference to ‘right’ is ambiguous
prog.cpp:3:15: error: candidates are: int right
In file included from /usr/include/c++/4.7/ios:43:0,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from prog.cpp:1:
/usr/include/c++/4.7/bits/ios_base.h:926:3: error:
std::ios_base& std::right(std::ios_base&)
Upvotes: 3
Views: 4549
Reputation: 22803
Observe the error message:
raw.cpp:105: error: reference to ‘right’ is ambiguous
raw.cpp:5: error: candidates are: int right
/usr/include/c++/4.2.1/bits/ios_base.h:917: error:
std::ios_base& std::right(std::ios_base&)
It's intimidating to read, but basically this is what it says:
raw.cpp:105: error: There's more than one ‘right’ here
One of them is yours: raw.cpp:5 int right
Another one isn't: <bits/ios_base.h:917>: some crap in namespace ‘std’
So left
and right
are already defined in namespace std
, which you are importing all of with using namespace std
. That's why you have an ambiguity. The simplest change to fix it is remove using namespace std;
and add using std::cin; using std::cout;
but this looks like too many global variables for my taste.
By the way, you should incorporate your code into your question. The question might be here longer than that paste, and my answer won't make sense if nobody can see the whole question.
Upvotes: 6