C2264将对象推回向量

时间:2016-05-26 21:27:05

标签: c++ vector

嘿伙计们,我正在制作一个棋盘游戏并实施它的表,直到我收到此错误。

class Grid {
private:
    int     size;
    char**  grid;
    int noofsupplies; //determined by the grid size.
    vector<Food*> food;
    vector<Water*>  water; //Used 4 different vector to keep track of every kind of object easier.
    vector<Mine*> mine;
    vector<Medical*> medical;
public:
    Grid(int sizeofgrid) {
        size = sizeofgrid;
        noofsupplies = 3 * (pow(sizeofgrid, 2) / 25);
        grid = new char*[size];
        for (int i = 0;i < size;i++)
            grid[i] = new char[size];
        for (int i = 0;i < size;i++) {
            for (int j = 0;j < size;j++)
                grid[i][j] = '-';
        }
        Resources* f = new Food();
        Resources* w = new Water();
        Resources* m = new Mine();
        Resources* s = new Medical();
        for (int i = 0;i < noofsupplies;i++) {
            food.push_back(*f);
            water.push_back(*w);
            mine.push_back(*m);
            medical.push_back(*s);
        }

        deploy_resources(); //place the food inside the map..
    }

但它给出了错误:

  

严重级代码说明项目文件行抑制状态错误   (active)没有重载函数的实例&#34; std :: vector&lt; _Ty,   _Alloc&gt; :: push_back [with _Ty = Food *,_ Alloc = std :: allocator]&#34;匹配参数列表

可能是什么原因?

2 个答案:

答案 0 :(得分:2)

food是指向Food的指针的向量,而不是Resources个对象的向量。

你可以这样做:

        Food* f = new Food();
        Water* w = new Water();
        Mine* m = new Mine();
        Medical* s = new Medical();
        for (int i = 0;i < noofsupplies;i++) {
            food.push_back(f);
            water.push_back(w);
            mine.push_back(m);
            medical.push_back(s);
        }

但是你只是将同一指针的noofsupplies个不同副本放在同一个指针上。只需执行

即可获得相同的效果
food.insert(food.begin(), noofsupplies, f);
water.insert(water.begin(), noofsupplies, w);
mine.insert(mine.begin(), noofsupplies, m);
medical.insert(medical.begin(), noofsupplies, s);

而不是循环。

但是多次拥有相同的物体可能不是你想要的。如果它们都应该是不同的对象,那么让你的循环完成

        for (int i = 0; i < noofsupplies; i++) {
            food.push_back(new Food());
            water.push_back(new Water());
            mine.push_back(new Mine());
            medical.push_back(new Medical());
        }

或者,如果他们根本不需要指针,只需要vector<Food>等,而不是指针的向量。

答案 1 :(得分:1)

问题是,您的push_back()来电没有推出各种vector所期望的正确值。

vector<Food*>希望您push_back() Food*,但是您要尝试推送Resource*

vector<Water*>希望您push_back() Water*,但是您要尝试推送Resource*

vector<Mine*>希望您push_back() Mine*,但是您要尝试推送Resource*

vector<Medical*>希望您push_back() Mine*,但是您要尝试推送Resource*

是的,您正在分别创建FoodWaterMineMedical类型的对象。但是您将所有这些变量分配给Resource*个变量,这就是错误源自的地方。您的vector没有人期待Resource*

请改为尝试:

class Grid {
private:
    int size;
    char** grid;
    int noofsupplies; //determined by the grid size.
    vector<Food*> food;
    vector<Water*> water;
    vector<Mine*> mine;
    vector<Medical*> medical;
public:
    Grid(int sizeofgrid)
        : size(0), grid(NULL), noofsupplies(0)
    {
        size = sizeofgrid;
        noofsupplies = 3 * ((sizeofgrid * sizeofgrid) / 25);
        grid = new char*[size];
        for (int i = 0; i < size;i++)
            grid[i] = new char[size];
        for (int i = 0; i < size; ++i) {
            for (int j = 0; j < size; ++j)
                grid[i][j] = '-';
        }
        for (int i = 0; i < noofsupplies; ++i) {
            food.push_back(new Food);
            water.push_back(new Water);
            mine.push_back(new Mine);
            medical.push_back(new Medical);
        }

        deploy_resources(); //place the food inside the map..
    }

    Grid(const Grid &src)
        : size(0), grid(NULL), noofsupplies(0)
    {
        size = src.size;
        noofsupplies = src.noofsupplies;
        grid = new char*[size];
        for (int i = 0; i < size; ++i)
            grid[i] = new char[size];
        for (int i = 0; i < size; ++i) {
            for (int j = 0; j < size; ++j)
                grid[i][j] = src.grid[i][j];
        }
        for (int i = 0; i < noofsupplies; ++i) {
            food.push_back(new Food(*(src.food[i])));
            water.push_back(new Water(*(src.water[i])));
            mine.push_back(new Mine(*(src.mine[i])));
            medical.push_back(new Medical(*(src.Medical[i])));
        }

        deploy_resources(); //place the food inside the map..
    }

    ~Grid() {
        for (int i = 0; i < size; ++i)
            delete[] grid[i];
        delete[] grid;
        for (int i = 0; i < noofsupplies; ++i) {
            delete food[i];
            delete water[i];
            delete mine[i];
            delete medical[i];
        }
    }

    Grid& operator=(Grid rhs) {
        swap(*this, rhs);
        return *this;
    }
};

通过删除所有newdelete调用,可以大大简化,让编译器和STL为您处理所有内存分配和发布:

class Grid {
private:
    vector<vector<char> > grid;
    vector<Food> food;
    vector<Water> water;
    vector<Mine> mine;
    vector<Medical> medical;
public:
    Grid(int sizeofgrid) {
        grid.resize(sizeofgrid);
        for (int i = 0; i < sizeofgrid; ++i)
            grid[i].assign(sizeofgrid, '-');

        int noofsupplies = 3 * ((sizeofgrid * sizeofgrid) / 25);
        food.resize(noofsupplies);
        water.resize(noofsupplies);
        mine.resize(noofsupplies);
        medical.resize(noofsupplies);

        deploy_resources(); //place the food inside the map..
    }
};