c++ - EXPECT_EQ 和过载错误

标签 c++ c++11 googletest

我正在使用谷歌测试功能 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);来 self 的上一篇文章: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);
    }

};

最佳答案

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

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

关于c++ - EXPECT_EQ 和过载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48290876/

相关文章:

C++如何泛化类函数参数来处理多种类型的函数指针?

c++ - 无法从 int[][] 转换为 int**?

c++ - 理解关于 c++11 的省略规则

c++ - 如果每个线程都有自己的只读数组拷贝,线程会更快吗

c++ - 使用加载链接/存储条件来防止 ABA 的无锁 C++11 示例?

c++ - 函数模板重载谜题

c++ - 如何创建用于使用 gtest 进行测试的 sqlite3_value

c++ - 未捕获派生异常类

c++ - 如何通过 Google 测试捕捉断言?

c++ - printf 和多个字符串的不良行为