3

我收到一个编译错误,说“左”和“右”不明确。

我是否在错误的地方声明了左,右?

  • 在 main 内部声明也无济于事
  • 在 main 上方移动函数定义无济于事

我将如何解决这个问题?

最小测试用例:

#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&)
4

1 回答 1

6

观察错误信息:

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’

所以leftright已经在 中定义namespace std,您将使用using namespace std. 这就是你有歧义的原因。修复它的最简单更改是删除using namespace std;和添加using std::cin; using std::cout;,但这看起来像我的口味的太多全局变量。

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.

于 2013-03-20T06:26:55.887 回答