2016-05-17 60 views
1

我有一個Shapes向量,Shape是我寫的類。在keyDown函數中,我遍歷這個Shapes矢量並將bool屬性background,更新爲true。但是,似乎並沒有堅持這一改變。C++(cinder):無法更新keyDown函數中的對象屬性

主類:

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事件,並正確地進行迭代矢量,b背景屬性仍然是錯誤的。我究竟做錯了什麼?

回答

1

嘗試

for (Shape &s : mTrackedShapes) 

您的代碼將使對象的副本,你將在矢量

+0

是在副本上更改屬性,而不是一個!非常感謝你,我不能相信我錯過了 – Kat