我收到一个编译错误,说'左'和'右'是不明确的。
我是否向左,右边错误地宣布了?
我该如何解决这个问题?
最低测试用例:
#include <iostream>
using namespace std;
int left = 0, right = 0;
int main()
{
cout << left;
cout << right;
}
正在给予:
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&)
答案 0 :(得分:4)
观察错误消息:
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&)
阅读令人生畏,但基本上这就是它所说的:
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’
所以left
和right
已在namespace std
中定义,您要导入using namespace std
。这就是你有歧义的原因。修复它的最简单的更改是删除using namespace std;
并添加using std::cin; using std::cout;
,但这看起来像我的口味太多全局变量。
顺便说一下,您应该将代码合并到您的问题中。问题可能比粘贴时间更长,如果没有人能看到整个问题,我的答案就没有意义了。