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

标签 c++ cinder

我有一个 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/

相关文章:

提供 `slice` 操作的 C++ 字符串类(不创建新的字符串拷贝)

c++ - CINDER_APP_BASIC 中的 Lib Cinder 方法设置{}

c++ - 如何创建 const 指针的动态数组?

c++ - Visual Studio 2012 C++ 标准输出

c++ - `vector<int> v;` 和 `vector<int> v = vector<int>();` 之间的区别

C++ - thread_local 变量存储在哪里?

c++ - 构造函数必须显式初始化没有默认构造函数的成员

opengl - 在 WebGL 中重现 Cinder 故障

c++ - C++/Cinder如何让窗口大小和打开的图片一样

c++ - 在 Cinder 中更改 alpha