0

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

};
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
user90823745
  • 149
  • 1
  • 2
  • 15

1 Answers1

1

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.

mrjaso
  • 51
  • 4
  • Hi, I added my code for class Man with the equality operator declaration, is it not implemented correctly?? – user90823745 Jan 16 '18 at 23:08
  • A few issues still. If you declare the equality operator inside the class definition, you need the declaration given by FrankS101. Also in your implementation, you use left == right, which is exactly what you are trying to define. Your implementation should compare each of the variables within the class ie. return username == right.username and firstname == right.firstname; etc. – mrjaso Jan 16 '18 at 23:17
  • You might want to read up here on defining operators: http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html – mrjaso Jan 16 '18 at 23:19
  • thanks for the link, I added the part to compare variables (updated code) but I still get the same error :( – user90823745 Jan 16 '18 at 23:55
  • Since you are defining in the class, you don't need both the left and right side parameters. The compiler will automatically take the left side of the equation as the calling object. (lh == rh) is the same as lh.operator==(rh). Your function signature should be: bool operator==(const Man& other) Then just compare firstname == other.first name (etc.) – mrjaso Jan 17 '18 at 00:40
  • Thank you so much, it works finally!! I didn't know there was notation like that and I probably would've never figured it out so thank you so much!! – user90823745 Jan 17 '18 at 00:52