EXPECT_EQ和重载错误

时间:2018-01-16 22:31:10

标签: c++ c++11 googletest

我正在使用google测试函数EXPECT_EQ来运行函数的测试用例。函数find返回list<MAN>并接收要查找的名称字符串。这是我的测试功能:

TEST_F(test_neighborhood, find) {
    list<Man> test;
    test.push_back(Man("username", "John", "Smith", 1, 1, ""));
    EXPECT_EQ(neighborhood.find("John"), test);
}

我了解到我必须在上一篇文章中添加bool operator ==(Man const & left, Man const & right);EXPECT_EQ Error,如下所示:

#include <string>
#include <queue>
#include <iostream>
#include <algorithm>

using namespace std;

class Man {
    ...
};
bool operator ==(Man const & left, Man const & right);

但是我收到了错误

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代码:

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);
    }

};

1 个答案:

答案 0 :(得分:1)

您要么没有实现等于运算符(这只是您已复制的声明),要么您没有编译已实现它的.cpp文件。

编译器会看到该函数的声明,并乐于继续编译,但链接器未在编译的代码中找到该函数。