gpt4 book ai didi

C++(煤渣): Can't update objects' properties in keyDown function

转载 作者:行者123 更新时间:2023-11-30 05:27:31 24 4
gpt4 key购买 nike

我有一个 Shapes vector ,Shape 是我写的一个类。在 keyDown 函数中,我遍历了这个 Shapes vector 并将 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;
}
}
}

形状.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;
};

形状.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)

您的代码将创建对象的拷贝,并且您将更改拷贝的属性而不是 vector 中的属性

关于C++(煤渣): Can't update objects' properties in keyDown function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37267033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com