C++ 预期_EQ和重载错误
我正在使用google测试函数C++ 预期_EQ和重载错误,c++,c++11,googletest,C++,C++11,Googletest,我正在使用google测试函数EXPECT\u EQ为函数运行测试用例。函数find返回一个列表,并接收要查找的名称字符串。下面是我的测试函数: TEST_F(test_neighborhood, find) { list<Man> test; test.push_back(Man("username", "John", "Smith", 1, 1, "")); EXPECT_EQ(neighborhood.find("John"), test); } 但是
EXPECT\u EQ
为函数运行测试用例。函数find
返回一个列表
,并接收要查找的名称字符串。下面是我的测试函数:
TEST_F(test_neighborhood, find) {
list<Man> test;
test.push_back(Man("username", "John", "Smith", 1, 1, ""));
EXPECT_EQ(neighborhood.find("John"), test);
}
但是我得到了错误
Undefined symbols for architecture x86_64:
"operator==(Man const&, Man const&)", referenced from:
testing::AssertionResult testing::internal::CmpHelperEQ<std::__1::list<Man, std::__1::allocator<Man> >, std::__1::list<Man, std::__1::allocator<Man> > >(char const*, char const*, std::__1::list<Man, std::__1::allocator<Man> > const&, std::__1::list<Man, std::__1::allocator<Man> > const&) in test_neighborhood.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [run] Error 1
您可能尚未实现相等运算符(这只是您复制的一个声明),或者没有编译在其中实现它的.cpp文件
<编译器>查看函数的声明,并乐于继续编译,但链接器在编译代码中找不到函数。< / P>您是否也实现了相等运算符?@ Fravs101对不起,我是C++新手;我该怎么做呢?friend bool操作符==(Man const&left,Man const&right){//如果左侧和右侧的Man对象相等,则返回}要了解更多详细信息,请发布与您的Man类相关的代码也为Man发布了我的类,添加friend函数Hi后,我仍然会收到相同的错误,我用相等运算符声明为类Man添加了代码,它是否没有正确实现?还有一些问题。如果在类定义中声明相等运算符,则需要FrankS101给出的声明。同样在您的实现中,您使用left==right,这正是您试图定义的。您的实现应该比较类中的每个变量,即返回username==right.username和firstname==right.firstname;等等。您可能想在这里阅读定义运算符的内容:感谢链接,我添加了比较变量的部分(更新的代码),但仍然得到相同的错误:(因为您是在类中定义的,所以不需要左侧和右侧参数。编译器将自动将等式的左侧作为调用对象。(lh==rh)与lh.operator==(rh)相同。您的函数签名应该是:bool operator==(const Man&other)然后比较firstname==other.firstname(等)
Undefined symbols for architecture x86_64:
"operator==(Man const&, Man const&)", referenced from:
testing::AssertionResult testing::internal::CmpHelperEQ<std::__1::list<Man, std::__1::allocator<Man> >, std::__1::list<Man, std::__1::allocator<Man> > >(char const*, char const*, std::__1::list<Man, std::__1::allocator<Man> > const&, std::__1::list<Man, std::__1::allocator<Man> > const&) in test_neighborhood.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [run] Error 1
class Man {
private:
string username;
string firstname;
string lastname;
int gender;
int age;
string tagline;
public:
Man();
Man(string _username, string _firstname, string _lastname,
int _gender, int _age, string _tagline);
string get_username();
string get_firstname();
string get_lastname();
int get_gender();
int get_age();
string get_tagline();
string get_info();
bool set_username(string _username);
bool set_firstname(string _firstname);
bool set_lastname(string _lastname);
bool set_gender(int _gender);
bool set_age(int _age);
bool set_tagline(string _tagline);
bool set_info(string _username, string _firstname, string _lastname,
int _age, string _tagline, int _gender);
// added this function in, but still getting the same error
bool operator==(const Man& _x, const Man& _y) const {
return (_x.username == _y.username) && (_x.firstname == _y.firstname) && (_x.lastname == _y.lastname) && (_x.gender == _y.gender) && (_x.age == _y.age) && (_x.tagline == _y.tagline);
}
};