2016-01-25 119 views
0

我在A類定義< <操作是這樣的: 啊:不能綁定 '的std :: basic_ostream' 左值到 '的std :: basic_ostream <char> &&'

class API_name A { 
    friend API_name std::ostream& operator<<(std::ostream& o, const A&a); 
} 

A.cpp:

ostream& operator<<(ostream& o, const A& a); 

在另一個文件中,包括嗯,我想這樣做:

void fonction(const A* a) { 
    std::cout << "a contains : " << *a << std::endl; 
} 

在這條線,我的gcc返回以下錯誤:

錯誤:無法綁定「的std :: basic_ostream」左值到「的std :: basic_ostream & &」

有人能告訴我爲什麼,以及如何避免這個編譯錯誤?

回答

1

friend定義錯誤地省略了&

也就是說,

class API_name A { 
    friend API_name std::ostream& operator<<(std::ostream o, const A&a); 
} 

應該

class API_name A { 
    friend API_name std::ostream& operator<<(std::ostream& o, const A&a); 
} 
+0

的和在定義mentionned。我在寫這個問題時犯了一個輸入錯誤,並且我收到了這個編譯錯誤。 – FlashMcQueen

+0

@FlashMcQueen:那麼問題不在於您發佈的代碼中,至少就我們所見。您可能需要發佈[mcve]。例如,什麼是「API_name」,以及你的類和函數是如何安排在名稱空間中的? – AndyG

+0

好的,我會這樣做的。 – FlashMcQueen

0

你的函數定義和執行不匹配。

  • A.H:std::ostream& operator<<(std::ostream o, const A&a);
  • A.cpp:stream& DTL::operator<<(ostream& o, const A& a);

你A.​​H文件需要更改爲:std::ostream& operator<<(std::ostream& o, const A&a);

相關問題