我无法使用迭代器访问地图内的数据。我想使用迭代器返回所有插入到地图中的值。但是,当我使用迭代器时,它不承认它已经进入的类实例中的任何成员。
int main()
{
ifstream inputFile;
int numberOfVertices;
char filename[30];
string tmp;
//the class the holds the graph
map<string, MapVertex*> mapGraph;
//input the filename
cout << "Input the filename of the graph: ";
cin >> filename;
inputFile.open(filename);
if (inputFile.good())
{
inputFile >> numberOfVertices;
inputFile.ignore();
for (int count = 0; count < numberOfVertices; count++)
{
getline(inputFile, tmp);
cout << "COUNT: " << count << " VALUE: " << tmp << endl;
MapVertex tmpVert;
tmpVert.setText(tmp);
mapGraph[tmp]=&tmpVert;
}
string a;
string connectTo[2];
while (!inputFile.eof())
{
//connectTo[0] and connectTo[1] are two strings that are behaving as keys
MapVertex* pointTo;
pointTo = mapGraph[connectTo[0]];
pointTo->addNeighbor(mapGraph[connectTo[1]]);
//map.find(connectTo[0]).addNeighbor(map.find(connectTo[1]));
//cout << connectTo[0] << "," << connectTo[1] << endl;
}
map<string,MapVertex*>::iterator it;
for (it=mapGraph.begin(); it!=mapGraph.end(); it++)
{
cout << it->getText() << endl;
}
}
return 0;
}
编译器输出:
\lab12\main.cpp||In function `int main()':|
\lab12\main.cpp|69|error: 'struct std::pair<const std::string, MapVertex*>'
has no member named 'getText'|
||=== Build finished: 1 errors, 0 warnings ===|
我的 MapVertex 类中有一个名为 getText() 的访问成员,它返回其中的数据。