0

I'm using the google test function EXPECT_EQ to run a test case for a function. The function, "find" returns a list 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);
}

But when I try to "make", it gives me a long error /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:665:71: error: invalid operands to binary expression ('const Man' and 'const Man') bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}

Am I not using EXPECT_EQ correctly? How do I fix this error?

user90823745
  • 149
  • 1
  • 2
  • 15

1 Answers1

1

EXPECT_EQ requires equality operator to be defined for passed items. std::list already has such an operator calling equality operator for each stored item. So it seems that you need to define operator == to compare two instances of Man class for equality:

bool operator ==(Man const & left, Man const & right)
user7860670
  • 35,849
  • 4
  • 58
  • 84
  • Hi, do I place that line in my test function? It still throws the same error I believe – user90823745 Jan 16 '18 at 21:01
  • You have to define it in the `Man` class – riqitang Jan 16 '18 at 21:02
  • No, you need to place it along with `Man` class definition, see [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading). – user7860670 Jan 16 '18 at 21:03
  • I think I'm still confused about where to place it (I placed in the Man's header class file within the class definition and tried both public and private) but I get this error in addition to the original: ./inc/man.h:21:10: error: overloaded 'operator==' must be a binary operator (has 3 parameters) bool operator ==(Man const & left, Man const & right); – user90823745 Jan 16 '18 at 21:10
  • @user90823745 It should be a free function in the same namespace. Typically it is declared either outside of class in the same header or inside of the class as a `friend`. – user7860670 Jan 16 '18 at 21:11
  • @VTT I put it outside and after the class definition in the header file but I get the error: Undefined symbols for architecture x86_64: "operator==(Person const&, Person const&)", referenced from:.... ld: symbol(s) not found for architecture x86_64 – user90823745 Jan 16 '18 at 21:18
  • @user90823745 is `Man` class actually called `Person`? Did you forget to write operator definition? – user7860670 Jan 16 '18 at 21:19
  • @VTT Sorry about that - I meant "operator==(Man const&, Man const&)", Person is a different object that basically has the same problem as this one – user90823745 Jan 16 '18 at 21:22