C++ (cinder):无法在 keyDown 函数中更新对象的属性
C++ (cinder): Can't update objects' properties in keyDown function
我有一个形状向量,形状是我写的class。在 keyDown 函数中,我遍历了这个 Shapes 向量并将 bool 属性、背景更新为 true。然而,它似乎并没有坚持这一改变。
主要class:
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事件,并正确地遍历了vector,但是背景属性仍然是false。我做错了什么?
尝试
for (Shape &s : mTrackedShapes)
您的代码将创建对象的副本,并且您将更改副本的属性而不是向量中的属性
我有一个形状向量,形状是我写的class。在 keyDown 函数中,我遍历了这个 Shapes 向量并将 bool 属性、背景更新为 true。然而,它似乎并没有坚持这一改变。
主要class:
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事件,并正确地遍历了vector,但是背景属性仍然是false。我做错了什么?
尝试
for (Shape &s : mTrackedShapes)
您的代码将创建对象的副本,并且您将更改副本的属性而不是向量中的属性