我有一个Shapes矢量,Shape是我写的一个类。在keyDown函数中,我遍历这个Shapes向量并将bool属性background更新为true。但是,似乎并没有坚持这种改变。
主要课程:
vector<Shape> mTrackedShapes;
void CeilingKinectApp::keyDown( KeyEvent event )
{
// remove all background shapes
if (event.getChar() == 'x') {
for (Shape s : mTrackedShapes) {
s.background = true;
}
}
}
Shape.h
#pragma once
#include "CinderOpenCV.h"
class Shape
{
public:
Shape();
int ID;
double area;
float depth;
cv::Point centroid; // center point of the shape
bool matchFound;
bool moving;
bool background;
cinder::Color color;
int stillness;
float motion;
cv::vector<cv::Point> hull; // stores point representing the hull of the shape
int lastFrameSeen;
};
Shape.cpp
#include "Shape.h"
Shape::Shape() :
centroid(cv::Point()),
ID(-1),
lastFrameSeen(-1),
matchFound(false),
moving(false),
background(false),
stillness(0),
motion(0.0f)
{
}
它注册keyDown事件,并正确迭代向量,但background属性仍为false。我做错了什么?
答案 0 :(得分:1)
尝试
for (Shape &s : mTrackedShapes)
您的代码会复制该对象,您将更改副本上的属性而不是向量中的属性