Search code examples
c++c++11googletest

EXPECT_EQ and overloading error


I'm using the google test function EXPECT_EQ to run a test case for a function. The function, find returns a list<MAN> and takes in a string of the name to find. Here's my test function:

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

I learned that I must include bool operator ==(Man const & left, Man const & right); from my last post: EXPECT_EQ Error which looks like this:

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

using namespace std;

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

But I get the error

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

If anyone could help explain the problem it'd be much appreciated!

EDIT - My code for 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);
    }

};

Solution

  • You either haven't implemented the equality operator (that's just a declaration you've copied) or you're not compiling the .cpp file that you've implemented it in.

    The compiler sees the declaration for the function and is happy to continue compiling, but the linker is not finding the function in the compiled code.