EXPECT_EQ和过载错误

EXPECT_EQ and overloading error

本文关键字:错误 EQ EXPECT      更新时间:2023-10-16

我正在使用谷歌测试函数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错误,如下所示:

#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 {
  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文件。

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