【发布时间】:2019-04-07 21:07:59
【问题描述】:
在我的程序中,我有一个包含整数类型向量的类。它用于存储距离。我有一个函数,当调用它时,应该将向量中的值设置为 0。 (用于初始化)。但是当我去检查向量的大小时,它仍然说向量是空的。
我创建了多个函数来检查向量是否添加了任何元素,而事实并非如此。我有一个函数,在 main 中,我调用它来查看向量是否为空,它返回 0(向量中有 0 个元素)。
int MLB::getDistanceSize()
{
return distances.size();
}
void MLB::setInitialDistances(int size)
{
for(int i = 0; i < size; i++)
{
this->distances.push_back(0);
}
}
class MLB
{
public:
//constructor
MLB();
~MLB();
int getDistanceSize();
void setInitialDistances(int size);
private:
vector<int> distances;
};
输入文件是一个csv文件,每一行包括:
体育场1,体育场2,距离
所以示例输入文件是:
AT&T Park,Safeco Field,680
AT&T Park,Oakland–Alameda County Coliseum,50
Angel Stadium,Petco Park,110
Angel Stadium,Dodger Stadium,50
Busch Stadium,Minute Maid Park,680
Busch Stadium,Great American Ball Park,310
Busch Stadium,Target Field,465
Busch Stadium,Kauffman Stadium,235
等等……
我正在使用 qt,这是我自己调用函数的地方。所有信息都存储在地图中,其他吸气剂工作得很好。很抱歉让这个问题比实际问题更令人困惑,非常感谢任何帮助。
// key and value, key is the team name, value is the MLB stadium information
struct entry
{
string key;
MLB value;
};
class Map
{
public:
//Public default constructor
Map();
//Public default destructor
~Map();
// returns entry of the map
entry atIndex(int index);
// Inserts a key and its value using linear algorithm
void insert(const string& theKey, const MLB& value);
private:
vector<entry> thisTable;
int currentSize; //Integer variable for current size
};
地图功能:
Map::Map()
{
currentSize = 0;
}
Map::~Map()
{
}
void Map::insert(const string& theKey, const MLB& value)
{
entry thisEntry;
thisEntry.key = theKey;
thisEntry.value = value;
thisTable.push_back(thisEntry);
currentSize+=1;
}
entry Map::atIndex(int index)
{
return thisTable.at(index);
}
//mainwindow constructor
mainWindow::mainWindow()
{
//Reads in input from first csv file, all works fine all data stored and can access it
string iStadium1;
string iStadium2;
string iDistance;
string previous;
int distance;
int index1;
int index2;
bool found;
ifstream csvFile2;
csvFile2.open("inputDistance.csv");
getline(csvFile2, iStadium1, ',');
while(!csvFile2.eof())
{
index1 = 0;
found = false;
while(!found)
{
if(thisMap.atIndex(index1).value.getStadiumName() == iStadium1)
{
thisMap.atIndex(index1).value.setInitialDistances(thisMap.mapSize());
cout << "Distance Size Test 1: " << thisMap.atIndex(index1).value.getDistanceSize() << endl;
found = true;
}
else
{
index1++;
}
}
previous = iStadium1;
while(iStadium1 == previous)
{
getline(csvFile2, iStadium2, ',');
getline(csvFile2, iDistance, '\n');
distance = stoi(iDistance);
index2 = 0;
found = false;
while(!found)
{
if(thisMap.atIndex(index2).value.getStadiumName() == iStadium2)
{
found = true;
cout << "Distance Size Test 2: " << thisMap.atIndex(index1).value.getDistanceSize() << endl;
// crashes here. Index out of bounds, size is 0 for some reason
thisMap.atIndex(index1).value.setDistance(index2, distance);
}
else
{
index2++;
}
}
getline(csvFile2, iStadium1, ',');
}
}
csvFile2.close();
}
我希望向量能够容纳 30 个值为 0 的槽(假设传递给函数的所需大小为 30),而不是一个空向量。
【问题讨论】:
-
是的,这就是您发布的代码的作用。因此,您看到的错误位于代码中的其他位置。您需要发布更多代码,尤其是检查向量大小的部分。很多时候(尤其是对于新手)错误不在他们认为的位置。
-
我把你的代码放到网上,cpp.sh/6d7aa。如您所见,它工作正常。
-
你是如何创建这个对象并在之后检查向量的?
-
在我的回答和提供的链接中,代码按照您的预期执行。有什么问题?
-
@john 我用调用函数本身的位置更新了我的代码。我正在使用 qt,当我从 csv 文件中读取以存储所有输入时,它可能有点复杂。如果您能看一下,将不胜感激
标签: c++ qt class vector setter