2014-01-15 2 views
1

나는 std::vector< std::vector<Tile> >을 보유하는 클래스 TileGrid을 가지고 있습니다. 벡터의 Tile 개체에 액세스해도 작동하지만 속성을 변경할 수 없습니까?왜 벡터의 객체를 변경할 수 없습니까?

#include "tilegrid.h" 

TileGrid::TileGrid() : rows_(0), cols_(0) { 
} 

TileGrid::TileGrid(unsigned int rows, unsigned int cols) : rows_(rows), cols_(cols) { 
    tiles_.clear(); 
    for (unsigned int y = 0; y < rows_; y++) { 
    std::vector<Tile> horizontalTiles; 
    for (unsigned int x = 0; x < cols_; x++) { 
     horizontalTiles.push_back(Tile()); 
    } 
    tiles_.push_back(horizontalTiles); 
    } 
} 

TileGrid::~TileGrid() { 
} 

tile.cpp

class Tile { 

public: 
    Tile(); 
    virtual ~Tile(); 
    bool isActive() const { return isActive_; }; 
    void setActive(bool status) { isActive_ = status; }; 

private: 
    bool isActive_; 

}; 

tile.h tilegrid.cpp
#include <vector> 
#include "tile.h" 

class TileGrid { 

public: 
    TileGrid(); 
    TileGrid(unsigned int rows, unsigned int cols); 
    virtual ~TileGrid(); 
    unsigned int getRows() const { return rows_; }; 
    unsigned int getCols() const { return cols_; }; 
    Tile atIndex(unsigned int row, unsigned int col) const { return tiles_[row].at(col); }; 

private: 
    std::vector< std::vector<Tile> > tiles_; 
    unsigned int rows_; 
    unsigned int cols_; 

}; 

tilegrid.h : 완성을 위해, 여기에 관련된 모든 클래스는

#include "tile.h" 

Tile::Tile() : isActive_(false) { 
} 

Tile::~Tile() { 
} 
나는이 모든 코드에 대한 정말 미안 해요 16,

MAIN.CPP

#include "tilegrid.h" 
#include <iostream> 

int main() { 

    TileGrid tg(20, 20); 

    for (unsigned int i = 0; i < tg.getRows(); i++) { 
    for (unsigned int j = 0; j < tg.getCols(); j++) { 
     if (tg.atIndex(i, j).isActive()) { 
     std::cout << i << "," << j << " is active" << std::endl; 
     } else { 
     std::cout << i << "," << j << " is NOT active" << std::endl; 
     } 
    } 
    } 

    // This is all working. But when I for example use the setActive function, nothing changes: 

    tg.atIndex(1, 0).setActive(true); 

    // When I print it again, the values are still the ones that were set in the constructor 

    for (unsigned int i = 0; i < tg.getRows(); i++) { 
    for (unsigned int j = 0; j < tg.getCols(); j++) { 
     if (tg.atIndex(i, j).isActive()) { 
     std::cout << i << "," << j << " is active" << std::endl; 
     } else { 
     std::cout << i << "," << j << " is NOT active" << std::endl; 
     } 
    } 
    } 

    return 0; 

} 

... 나는 가능한 한 짧게 유지하려고하지만 난 그것을 모두를 게시하는 것이 더 줄 알았는데!

그래, 내 문제는 setActive 기능입니다. Tile을 작성하고 setActive 함수를 호출하면 모든 것이 작동하지만, TileGrid 오브젝트를 통해 호출하면 작동하지 않습니다.

저는 몇 시간 동안이 문제를 직접 풀려고했는데 더 이상 똑바로 생각할 수 없습니다. 나는 정말 절망적이다. 좀 봐 주시고 저를 도울 수 있습니까? 당신의 방법에서

답변

9

:

Tile atIndex(unsigned int row, unsigned int col) const 

당신이 타일에 대한 참조를 반환해야합니다 :

Tile& atIndex(unsigned int row, unsigned int col) 

지금 당신이 반환하는 사본을, 및 수정이 작동하지 않는 이유입니다. 또한 const이 아니어야합니다. 그렇지 않으면 컴파일러 오류가 발생합니다.

+0

const 및 비 const 둘 모두가 필요할 수 있습니다. const one은 const 참조를 반환합니다. – Roddy

+0

일부 컨텍스트에서는 const 버전 인'const Tile & atIndex (unsigned int row, unsigned int col) const'가 필요할 수도 있습니다. – marcinj

+0

오, 정말 고마워. <3 참조를 사용하려고했지만이 어리석은 const는 항상 비밀스러운 컴파일러 오류를 주었고 무엇을 바꾸어야할지 몰랐다. 나는 당황 스럽다. –

관련 문제