矢量对象未正确设置向量中的信息(c ++)

时间:2017-08-09 01:03:55

标签: c++ pointers object vector polymorphism

Heyo!我对C ++很新,但在编码方面有一些背景,所以更简单的解释会很棒。我一直在研究一个相当复杂的程序,出现了一个问题。我有一个名为“Tile”的对象是在向量中制作的。

for (int i = 0; i <= ySize; i++) {
    for (int i2 = 0; i2 <= xSize; i2++) {
        tiles.push_back(make_unique <Tile>());
        tiles[i*xSize + i2]->set_height(fn.GetNoise(i2, i));
        tiles[i*xSize + i2]->set_xPos(i2);
        tiles[i*xSize + i2]->set_yPos(i);
        tiles[i*xSize + i2]->update_tile();
    }
    cout << endl;
}

理想情况下,瓷砖以网格方式生成。在tile对象中是一个名为“goods”的向量。

vector unique_ptr<Good*> goods;

这些商品通过双循环添加:

for (int i = 0; i <= ySize; i++) {
    for (int i2 = 0; i2 <= xSize; i2++) {
        if (tiles[i*xSize + i2]->get_rainfall() >= 0.1 && tiles[i*xSize + i2]->get_height() >= -0.2 && tiles[i*xSize + i2]->get_height() <= 0.55 && fn.GetNoise(i2, i) < 0) {
            tiles[i*xSize + i2]->add_available_good(1001, (fn.GetNoise(i, i2)*500+214));
            tiles[i*xSize + i2]->update_tile();
        }
    }
}

此向量将通过此处存储Good类的子类:

void Tile::add_available_good(short unsigned int g, short int a) {
//Adds good based on GoodID
switch (g) {
case 1001:
    goods.emplace_back(new Tree(a));
    break;
case 1002:

    break;
default:

    break;
}

}

出现的问题是每当我调用瓷砖时

[i*xSize + i2]->add_available_good(num, num);

有点好笑。任何给定瓷砖上的矢量货物的大小变为添加到所有瓷砖的货物数量。换句话说,看起来货物矢量在所有瓷砖对象之间共享。我该如何解决?

我很难解释事情,但我尽力了。提前谢谢!

瓷砖类定义:

#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
#include <locale>
#include <random>
#include <memory>

using namespace std;

//Misc inclusions
#include "Building.h"
#include "Good.h"

class Tile
{
public:
Tile();
void print_tile();
void set_tile_symbol(char);
char get_tile_symbol();
void set_tile_color(float);
float get_tile_color();
void add_building(short unsigned int);
void set_height(double);
double get_height();
void update_tile();
void add_available_good(short unsigned int, short int);
void set_rainfall(double);
double get_rainfall();
void set_xPos(int);
void set_yPos(int);
short unsigned int get_good(short unsigned int);
short unsigned int get_good_weight(short unsigned int);
void print_good();
~Tile();
private:

char symbol;
int xpos;
int ypos;
float color;
short unsigned int usableSpace = 36;
double height = 0;
int pop = 0;
bool hasPop = 0;
double rainfall = 0;
};

良好的课程定义:

class Good
{
public:
Good();
void set_weight(double);
void change_weight(double);
double get_weight();        
void set_value(double);
double get_value();
void change_value(double);
short unsigned int get_symbolShowsAt();
char get_symbol();
short unsigned int get_goodID();
~Good();    
protected:
double weight;
short unsigned int reqFunction[5];
short unsigned int reqGoodID[5];
short unsigned int goodWeight[5];
double value = 1;
short unsigned int goodID = 1000;
short unsigned int functionID = 4000;
bool hasSymbol = 0;
short unsigned int symbolShowsAt = 100;
char symbol = 0;
int size = 1;
};
struct Tree : public Good { Tree(short unsigned int w); };

1 个答案:

答案 0 :(得分:-1)

将全局矢量设置为成员。