2013-03-20 49 views
2

我收到編譯錯誤,說'左'和'右'不明確。編譯錯誤:左邊模糊不清,右邊模糊

我是否宣佈左,右對錯?

  • 內主聲明並沒有幫助
  • 移動上述主要功能的定義並不能幫助

如何我會解決這個問題?

最少測試用例:

#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&) 
+3

對未來的建議 - 構建[最小測試用例](https://ideone.com/c2DqtQ)並將代碼粘貼到問題中,而不僅僅是指向它的鏈接。 – Dukeling 2013-03-20 06:30:06

+0

我只是無法理解此網站上的代碼格式! – user1776433 2013-03-20 06:36:54

+0

現在你知道你爲什麼不使用'namespace std;'! – 2013-03-20 06:36:58

回答

3

觀察錯誤消息:

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;,但這看起來像是我的口味中的太多全局變量。

順便說一句,你應該把你的代碼併入你的問題。這個問題在這裏可能會比粘貼時間更長,如果沒有人能看到整個問題,我的回答就沒有意義了。

+0

我不知道左和右是在命名空間std中定義的,我將它們更改爲lft和rght。非常感謝。 – user1776433 2013-03-20 06:34:52

+1

如果你在我的班級,我會給你寫一個字母等級,只是爲了這些新的變量名。 'left_index'或'deque_left'怎麼樣? – 2013-03-20 06:38:02

+0

或者不要使用'using namespace std;',而是使用'std ::'前綴你想要的項目,或者指定你從'std'導入的內容。 (我認爲':: left'和':: right'也會起作用;不過我不確定我會推薦這個符號。) – 2013-03-20 06:41:06