在C ++ Map中将对象(定义的类)存储为键

这是上一个问题的后续内容,但是我不想编辑该帖子,所以我做了一个新帖子。

我有一个C ++ xlApp2 = new Excel.Application(); xlWb2 = xlApp2.Workbooks.Add(); xlWs2 = xlWb2.Worksheets.get_Item(1); xlWs2.Name = "Format OR"; for (int j = 1; j < rowor; j++) { xlWb2.Sheets[1].activate(); if (fulldataOR[j][23] != "RJCT") { createHeaderformatOR(xlWs2); xlWs2.Cells[berhasilOr,1] = fulldataOR[j][2]; xlWs2.Cells[berhasilOr,2] = fulldataOR[j][3]; xlWs2.Cells[berhasilOr,3] = fulldataOR[j][4]; xlWs2.Cells[berhasilOr,4] = fulldataOR[j][5]; xlWs2.Cells[berhasilOr,5] = fulldataOR[j][6]; xlWs2.Cells[berhasilOr,6] = fulldataOR[j][7]; xlWs2.Cells[berhasilOr,7] = fulldataOR[j][8]; xlWs2.Cells[berhasilOr,8] = fulldataOR[j][9]; xlWs2.Cells[berhasilOr,9] = fulldataOR[j][12]; xlWs2.Cells[berhasilOr,10] = fulldataOR[j][13]; xlWs2.Cells[berhasilOr,11] = fulldataOR[j][14]; xlWs2.Cells[berhasilOr,12] = fulldataOR[j][15]; xlWs2.Cells[berhasilOr,13] = fulldataOR[j][16]; xlWs2.Cells[berhasilOr,14] = fulldataOR[j][17]; xlWs2.Cells[berhasilOr,15] = fulldataOR[j][18]; xlWs2.Cells[berhasilOr,16] = fulldataOR[j][19]; xlWs2.Cells[berhasilOr,17] = fulldataOR[j][20]; xlWs2.Columns.AutoFit(); berhasilOr++; } } xlWs3 = xlWb2.Worksheets.get_Item(2); xlWs3.Name = "PBK - USD"; for (int j = 1; j < rowusd; j++) { xlWb2.Sheets[2].activate(); if (fulldataUSD[j][9] == "USD") { if (fulldataUSD[j][11] != "RJCT") { //MessageBox.Show(fulldataUSD[j][1]); createHeaderPBKUSD2(xlWs3); xlWs3.Cells[berhasilusd,1] = fulldataUSD[j][4]; xlWs3.Cells[berhasilusd,2] = fulldataUSD[j][5]; xlWs3.Cells[berhasilusd,3] = ""; xlWs3.Cells[berhasilusd,4] = ""; xlWs3.Cells[berhasilusd,5] = ""; xlWs3.Cells[berhasilusd,6] = ""; xlWs3.Cells[berhasilusd,7] = ""; xlWs3.Cells[berhasilusd,8] = ""; xlWs3.Cells[berhasilusd,9] = ""; xlWs3.Cells[berhasilusd,10] = ""; xlWs3.Cells[berhasilusd,11] = fulldataUSD[j][3]; xlWs3.Cells[berhasilusd,12] = ""; xlWs3.Cells[berhasilusd,13] = fulldataUSD[j][2]; xlWs3.Cells[berhasilusd,14] = fulldataUSD[j][9]; xlWs3.Cells[berhasilusd,15] = fulldataUSD[j][6]; xlWs3.Cells[berhasilusd,16] = ""; xlWs3.Cells[berhasilusd,17] = fulldataUSD[j][7]; xlWs3.Cells[berhasilusd,18] = ""; xlWs3.Cells[berhasilusd,19] = ""; berhasilusd++; xlWs3.Columns.AutoFit(); } } } ,用于存储有关已连接组件的信息。这是我的std::map类中的代码段,非常基础

BaseStation

在我的主要代码中,我有一个地图声明为

//Constructor BaseStation(string name,int x,int y){ id = name; xpos = x; ypos = y; } //accessors string getName() const{ return id; }

map<BaseStation,vector<string> > connection_map;会随着在while循环中从文件中读取数据而更新,然后在此之后,出于我自己的调试目的,我想转储映射的内容。我将BaseStation对象附加到地图上(作为键),并将其作为值附加到BaseStation对象的链接列表:

connection_map

现在尝试编译时,出现以下错误:

.....
    (while loop file read in...)

    connection_map[BaseStation(station_name,x,y)] = list_of_links; 
    list_of_links.clear();
}

for(auto ptr = connection_map.begin(); ptr != connection_map.end(); ++ptr){
    cout << ptr->first.getName() << " has the following list: ";
    vector<string> list = ptr->second;
    for(int i = 0; i < list.size(); i++){
        cout << list[i] << " ";
    }
    cout << endl;
}

经过冗长的堆栈跟踪之后,我在这里找到了问题的根源:

error: invalid operands to binary expression ('const BaseStation' 
and 'const BaseStation')

我不明白为什么您不能将对象存储为键。如果这在语法上不是正确的C ++,我将如何在地图中存储基站及其信息?我不要任何多余的信息

mgdbrrr 回答:在C ++ Map中将对象(定义的类)存储为键

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3120349.html

大家都在问