没有从矢量获得正确的对象

时间:2015-03-15 08:19:35

标签: c++

我在从行中获取正确的对象时遇到问题

  

Cell cF = fList [rand()%fList.size()]; //选择随机前沿单元格

     

Cell cl = inList [rand()%inList.size()]; //在单元格中选择随机

每当我使用visual studio调试时,我都可以看到Cell会因为方法pushToPrimsFrontierList(Cell& c)而添加它的成员,但是当我尝试从inList或fList中获取对象时,似乎我不是得到相同的对象引用,因为它的邻居列表再次为0。这里发生了什么?

你可以从图像中看到,在第一次迭代中,一个startCell被添加到inList中,所以当我访问它时它只返回一个对象,但不是这样,看起来我的对象不是甚至push_backed到inList向量。 enter image description here

#ifndef __CELL_H_
#define __CELL_H_
#include <vector>
using namespace std;

class Cell{
private:
    int x, y, val;    // cell co-ordinate
    bool visited;
    void setX(int);
    void setY(int);
    vector<Cell> neighbourList;
public: 
    Cell();
    Cell(int,int);
    int getX();
    int getY();
    int getVal();
    vector<Cell>& getNeighbourList();
    void pushToNeighbourList(Cell&);
    void setVal(int);
    void setVisited(bool);
    bool IsVisited();
    bool equals(Cell&);
};

#endif

#ifndef GENMAZE_H
#define GENMAZE_H
#include <vector>
#include <iostream>
#include "cell.h"
using namespace std;

class GenMaze{
private:
    int rows;
    int cols;
    int gridSize;
    vector<vector<Cell>> mazeGrid;
    vector<Cell> inList;
    vector<Cell> fList;
public:
    GenMaze(int,int);
    void setRows(int);
    void setCols(int);
    void setGridSize(int);
    int getGridSize();
    int getRows();
    int getCols();
    vector<vector<Cell>>& getMazeGrid();
    void setMazeGrid(vector<vector<Cell>>);
    void setValAt(int,int, Cell);
    void printMazeCoords();
    void printMazeValue();
    bool isOddBlock(int,int);
    void Prims();
    void pushToPrimsFrontierList(Cell&);
    void printCell(Cell&);
    void makePath(Cell&, Cell&);
    void removeFromfList(Cell&);
};
#endif


void GenMaze::Prims(){

    Cell startCell = mazeGrid[1][1];//startCell
    inList.push_back(startCell);
    pushToPrimsFrontierList(startCell);
    int randomFrontier= 0;
    int randomIn = 0;

    while (!fList.empty()){
        cout<< "e";
        Cell cF = fList[rand() % fList.size()];//choose random frontier cell
        Cell cl = inList[rand() % inList.size()];//choose random in cell
        for (vector<Cell>::size_type i = 0; i != cl.getNeighbourList().size(); i++) {
            if (cl.getNeighbourList()[i].equals(cF)){
                inList.push_back(cF);
                pushToPrimsFrontierList(cF);
                makePath(cl, cF);
                removeFromfList(cF);
            }
        }
    }
}
void GenMaze::removeFromfList(Cell& c){
    for (vector<Cell>::size_type i = 0; i != fList.size(); i++) {
        if (fList[i].equals(c)){
            fList.erase(fList.begin() + i);
        }
    }

}
void GenMaze::makePath(Cell& from, Cell& to){
    cout << "making path";
    //on top
    if ((from.getX() - 2 == to.getX()) & (from.getY() == to.getY())){
        mazeGrid[from.getX() - 1][from.getY()].setVal(0);
    }

    //on right
    if ((from.getX()  == to.getX()) & (from.getY() + 2 == to.getY())){
        mazeGrid[to.getX()][from.getY() - 1].setVal(0);
    }

    //on bottom
    if ((from.getX() + 2 == to.getX()) & (from.getY() == to.getY())){
        mazeGrid[from.getX() + 1][from.getY()].setVal(0);
    }

    //on left
    if ((from.getX()  == to.getX()) & (from.getY() - 2 == to.getY())){
        mazeGrid[from.getX()][from.getY() - 1].setVal(0);
    }


}
void GenMaze::printCell(Cell& c){
    cout << "(" << c.getX() << "," << c.getY() << ")";
}
void GenMaze::pushToPrimsFrontierList(Cell& c){
    //push all Cells around the given Cell c, into the frontier list.

    if (!(c.getX() - 2 < 0)){
        Cell topCell = mazeGrid[c.getX() - 2][c.getY()];
            fList.push_back(topCell);
            c.pushToNeighbourList(topCell);
        }

        if (!(c.getY() - 2 < 0)){
            Cell leftCell = mazeGrid[c.getX()][c.getY() - 2];
            fList.push_back(leftCell);
            c.pushToNeighbourList(leftCell);
        }

        if (!(c.getY() + 2 > getCols() - 1)){
            Cell rightCell = mazeGrid[c.getX()][c.getY() + 2];
            fList.push_back(rightCell);
            c.pushToNeighbourList(rightCell);
        }

        if (!(c.getX() + 2 > getRows() - 1)){
            Cell bottomCell = mazeGrid[c.getX() + 2][c.getY()];
            fList.push_back(bottomCell);
            c.pushToNeighbourList(bottomCell);
        }       
}

1 个答案:

答案 0 :(得分:0)

std::vector几乎与每个STL集合一样,您无法将引用放在对象上。如果你这样做:

Cell c;
std::vector<Cell> myvector1;
std::vector<Cell> myvector2;

myvector1.push_back(c);
myvector2.push_back(c);

当您尝试修改c中的myvector1时,该值不会传播到myvector2。这是因为push_back按值而不是通过引用添加元素。如果你需要对某个对象的实际引用,你应该创建指向元素的指针集合,代码应该是这样的:

Cell *c = new Cell; 
std::vector<Cell*> myvector1;
std::vector<Cell*> myvector2;

myvector1.push_back(c);
myvector2.push_back(c);

现在,当您想要修改元素beneith c时,您只需:

myvector1[indexofc]->somecellfield = othervalue