[英]Replace object in vector using iterator
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
. 总的来说,我正在遍历
persons
向量,当找到匹配项时,我想将rect
更新为某个新的rect
甚至替换整个新的对象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. 我试图
erase
该对象并添加一个新对象,但是我仍然遇到相同的错误。 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. 我已经阅读了C ++从矢量中删除对象,但是不确定这是否是我的问题,并且我没有使用C ++ 11,所以这些解决方案对我不起作用。
Is it something with the iterator and my person
object when they come to comparison? 迭代器和我的
person
对象进行比较时是否有问题? I think it is but no idea how to solve it. 我认为这是解决办法,但不知道。
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: 如果要比较
person
类型的对象与Rect
类型的对象(这是您要replace
含义),则必须在Person
类中提供适当的比较运算符,例如:
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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.