2016-01-25 78 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);

相关问题