3

I hope the title describes my problem completely.

Running the code I get an error:

error C2678: binary '==':no operator found which takes a left-hand operand of tpye 'A' (or there is no acceptable conversion)"

Where is the mistake and how can I fix the problem???

class A
{
  private: //Dummy Values
    int x;
    int y;
}

class B
{
  private:
    vector <A> dataHandler;

  public:
    bool isElement(A element);
    //Should return true if element exists in dataHandler
}

bool B::isElement(A element)
{
  int length = dataHandler.size();

  for(int i = 0; i<length; i++)
    {
      if(dataHandler[i] == element) //Check if element is in dataHandler
        return true;
    }
  return false;
}
dlavila
  • 1,204
  • 11
  • 25
RefMa77
  • 283
  • 2
  • 14
  • You should either store your instances by pointer, or implement the comparison `operator==` for `class A`. – cfh May 27 '15 at 13:41
  • possible duplicate of [C++ object equality](http://stackoverflow.com/questions/16843323/c-object-equality) – Christian Sarofeen May 27 '15 at 13:43
  • @cfh: "store your instances by pointer" - that makes no sense. If you're storing pointers, you're not storing instances. – Mike Seymour May 27 '15 at 13:44
  • @MikeSeymour: Obviously, I meant storing pointers to the instances. – cfh May 27 '15 at 13:44
  • 2
    @cfh: Which instances? Where are you storing them if you're storing pointers in the vector? Your "simple" suggestion opens up a huge can of worms, changing a very simple data structure into something much harder to deal with. – Mike Seymour May 27 '15 at 13:47
  • @MikeSeymour: And yet it may be the proper choice in some situations. We don't know enough about the context of the question. – cfh May 27 '15 at 13:48

4 Answers4

4

Within isElement you have

if(dataHandler[i] == element)

This is attempting to compare two A instances using operator==, but your A class doesn't implement any such operator overload. You probably want to implement one similar to this

class A
{
  private: //Dummy Values
    int x;
    int y;
  public:
    bool operator==(A const& other) const
    {
      return x == other.x && y == other.y;
    }
};

Also, isElement can be rewritten using std::find instead of a for loop

bool B::isElement(A const& element) const
{
  return std::find(dataHandler.begin(), dataHandler.end(), element) != dataHandler.end();
}
Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • +1 It works, thank you. I wrote a similar operator method just without the two consts and the & and it did not work, but why do I have to do that? – RefMa77 May 28 '15 at 06:30
  • @RefMa77 If the parameter type is just `A`, then you'll make a copy of the argument to `operator==`, which is unnecessary in this case. The last `const` indicates that the function does not [modify any class members](https://stackoverflow.com/a/751783/241631). But that [should work](http://coliru.stacked-crooked.com/a/c067d6bdfb8f4a28) unless you were trying to call `isElement` on a `const` instance of `B`. Can you post an example that reproduces the error? – Praetorian May 28 '15 at 13:09
  • I did a huge mistake bye just comparing the instances, I have to compare every single variable. So everything is fine now :D – RefMa77 May 28 '15 at 13:56
2

Compiler tells you everything. Define operator== for class A. Update class A to something like this:

class A
{
  private: //Dummy Values
    int x;
    int y;
  public:
    bool operator==(A const& rhs) const
    {
      return x == rhs.x && y == rhs.y;
    }
};
dlavila
  • 1,204
  • 11
  • 25
banach-space
  • 1,781
  • 1
  • 12
  • 27
0

you have to write your own == operator for class A, something like

bool operator==(const A &rhs) const
{
    return this->x == rhs.x && this->y == rhs.y;
}

otherwise there's no way to know how to compare A objects.

dlavila
  • 1,204
  • 11
  • 25
0

You will have to implement the operator==.

Example of operator== (inline non-member function):

inline bool operator== (const A& left, const A& right){ 
    return left.getX() == right.getX() && left.getY() == right.getY();
}
Jérôme
  • 8,016
  • 4
  • 29
  • 35