-1

This is my object class:

class person
{
public:

    int id;
    Rect rect;
};

In main, I am iterating through vector of persons and when I find a match, I want to update rect to some new rect or even replace the entire new object person.

Rect mr = boundingRect(Mat(*itc));
person per;
vector <person> persons;
vector <person>::iterator i;

i = persons.begin();
while (i != persons.end()) {
    if ((mr & i->rect).area() > 0) {
        rectangle(frame, mr, CV_RGB(255, 0, 0));
        putText(frame, std::to_string(i->id).c_str(), mr.br(),
            FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 255));

        replace(persons.begin(), persons.end(), i->rect, mr); // this line causes error
        break;
    } else {
      ...
    }

The error I am getting at the line I marked by comment is:

Error   C2678   binary '==': no operator found which takes a left-hand operand of type 'person' (or there is no acceptable conversion)

and also this one:

Error   C2679   binary '=': no operator found which takes a right-hand operand of type 'const _Ty' (or there is no acceptable conversion)   

I have tried to erase the object and add a new one but I was still getting the same error. I have read C++ Remove object from vector but I am not sure if this is my problem and I am not using C++11 so these solutions don't work for me.

Is it something with the iterator and my person object when they come to comparison? I think it is but no idea how to solve it.

mrRobot
  • 301
  • 3
  • 12
  • Can't you do `i->rect = mr;`? – tkausl Apr 03 '19 at 22:04
  • I could but I also need to use `replace` or `remove` the object...and this error occurs for both...I mean there is a workaround as you suggested, but I am looking for solution how to fix it right way. – mrRobot Apr 03 '19 at 22:08
  • If you just want to change the `rect` member of the current `person` object inside the vector, and keep things in the same order, you don't need to do any removing or replacing. Just `i->rect = mr;` will do exactly that, because `*i` is the object in the vector, not a copy of it. If that's not what you want, please edit the post to more exactly describe what you're trying to accomplish. – aschepler Apr 03 '19 at 22:34
  • 2
    @mrRobot I didn't downvote, but please take a look at [Why isn't providing feedback mandatory on downvotes, and why are ideas suggesting such shot down?](https://meta.stackoverflow.com/q/357436/1288408) Comments on downvotes are not mandatory for many reasons. It's not trollish behavior to leave no comment. – Modus Tollens Apr 03 '19 at 22:53

1 Answers1

1

If you want to compare an object of type person with an object of type Rect (which is what your call to replace implies), then you must provide an appropriate comparison operator to do so in your Person class, like this:

bool operator== (const Rect &r) const { ... }

Similarly, you need an assignment operator with a signature (and probable implementation) like this:

person& operator= (const Rect &r) { rect = r; return *this; }

Simplified live demo

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Doesn't my call to `replace` imply that I am comparing `person->rect` with `rect` ? Isn't this the same ? – mrRobot Apr 03 '19 at 22:26
  • No, it tries to compare `person` with `rect`, hence your difficulties, but knowing that should help you write your `operator==` function. – Paul Sanders Apr 03 '19 at 22:27
  • And if I want to `erase` object where the iterator is pointing, how do I do it? Because I have tried it and I got same error. – mrRobot Apr 03 '19 at 22:33
  • Please post that as a new question, showing your code. Try and post a [MCVE]. – Paul Sanders Apr 03 '19 at 22:35