2013-09-27 57 views
0

测试两个std::pairBOOST单元测试覆盖运营商<<

BOOST_CHECK_EQUAL(std::make_pair(0.0,0.0), std::make_pair(1.0,1.0)); 

我重载operator<<std::pair

std::ostream& operator<< (std::ostream& os, const std::pair<double,double>& t) 
{ 
    return os << "(" << t.first << ", " << t.second << ")"; 
} 

与以下错误

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::pair<_Ty1,_Ty2>' (or there is no acceptable conversion) 

有什么不对?

+1

您可能是指“过载”,而不是“覆盖”。 – Elazar

+0

@Jesse好,我测试你的解决方案,命名空间arround,它的工作原理(但我不知道为什么) –

+0

@Behelke:自从它工作后,我取消了我的回答。试图弄清楚究竟发生了什么。 –

回答

2

打开std namespace,ADL可以找到它。

namespace std 
{ 
ostream& operator<< (ostream& os, const pair<double,double>& t) 
{ 
    return os << "(" << t.first << ", " << t.second << ")"; 
} 
} 

好的,我想通了。名称查找停止当它找到它正在寻找在当前名字空间,这就是为什么它不能找到在全球范围内的operator<<名称,因为它在namespace boost发现operator<<已经因为升压声明operator<<

我推荐阅读Why can't I instantiate operator<<(ostream&, vector&) with T=vector?这有一个很好的解释。

+0

是否需要编写'std :: ostream'和'std :: pair'?难道不是意味着'std :: std :: pair'? – Elazar

+0

不需要写std :: ostream –

+1

@Behelke:我想明白了为什么现在看到我更新的答案。 –