EXPECT_EQ 和重载错误

EXPECT_EQ and overloading error

我正在使用 google 测试函数 EXPECT_EQ 来 运行 函数的测试用例。函数 find returns a 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);
}

我了解到我必须包括我上次 post 中的 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 人代码:

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

};

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

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