我遇到了Boost C ++单元测试框架的问题。这是 someclass.h文件
class someclass {
public:
someclass(int a_x, int b_x);
someclass();
friend bool operator<(someclass& lhs, someclass& rhs);
friend ostream& operator<<(ostream& stream, const someclass& sa);
private:
int a;
int b;
};
这是someclass.cpp文件
#include <iostream>
#include <ostream>
#include <vector>
#include <tuple>
using std::cout;
using std::endl;
using std::vector;
using std::ostream;
#include "someclass.h"
someclass::someclass(int a_x, int b_x)
:a(a_x), b(b_x)
{
}
//-------------------------------------------------------
someclass::someclass()
{
}
//-------------------------------------------------------
bool operator<(someclass& lhs, someclass& rhs)
{
return std::tie(lhs.a, lhs.b) < std::tie(rhs.a, rhs.b);
}
//-------------------------------------------------------
ostream& operator<<(ostream& stream, const someclass& rhs)
{
stream << "(" << rhs.a << "," << rhs.b << ")";
return stream;
}
以下是测试它的程序:
#define BOOST_TEST_MODULE test_someclass
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <ostream>
using std::ostream;
#include "someclass.h"
BOOST_AUTO_TEST_CASE( someclass_lt )
{
someclass sa1(1,4);
someclass sa2(1,5);
someclass sa3(2,3);
someclass sa4(2,4);
someclass sa5(2,5);
BOOST_TEST( sa1 < sa2 );
BOOST_TEST( !(sa2 < sa1) );
BOOST_TEST( sa1 < sa3 );
BOOST_TEST( sa1 < sa4 );
BOOST_TEST( sa1 < sa5 );
}
someclass.cpp文件本身编译得很好,但是当我尝试编译时 整个事情我得到了一大堆错误信息,部分是
In file included from testsomeclass.cpp:9:0:
someclass.h:6:19: note: bool operator<(someclass&, someclass&)
friend bool operator<(someclass& lhs, someclass& rhs);
^
someclass.h:6:19: note: no known conversion for argument 1 from ‘someclass’ to ‘someclass&’
我已经尝试过每一种我能想到的方法来纠正这一点,但无济于事。 我也在stackoverflow.com和许多其他网站上搜索,但无济于事。 有人可以告诉我什么是错的,并且确切地说如何修复它?感谢。
布鲁斯
答案 0 :(得分:0)
你应该使用正确的constness:
friend bool operator<(const someclass& lhs, const someclass& rhs);