c通过引用将地图传递给函数
发布时间:2020-12-16 05:38:40 所属栏目:百科 来源:网络整理
导读:如何通过引用将地图传递给函数? Visual Studio 2010给我一个未解决的外部错误.目前,我有以下简化代码: void function1(){ mapint,int * my_map = new mapint,int(); function2(*my_map); }void function2(mapint,int temp_map){ //do stuff with the map}
如何通过引用将地图传递给函数? Visual Studio 2010给我一个未解决的外部错误.目前,我有以下简化代码:
void function1(){ map<int,int> * my_map = new map<int,int>(); function2(*my_map); } void function2(map<int,int> &temp_map){ //do stuff with the map } 这里有类似问题的几个答案,但是他们使用typedef并将std ::添加到定义的开头,但我真的不知道为什么. int ComputerPlayer::getBestMoves(){ //will return the pit number of the best possible move. //map to hold pit numbers and rankings for each possible pit number. //map<pitNumber,rank> only adds pit numbers to map if they have seeds in them. std::map<int,int> possiblePits; //map std::map<int,int>::iterator it; //iterator for map for(int index = 1; index <= getBoardSize(); index++){ if(_board.getPitValue(index) > 0){ possiblePits.insert( pair<int,int>(index,0) ); } } int tempBoardSize = _board.getBoardSize(); //loop that will analyze all possible pits in the map for(it = possiblePits.begin(); it != possiblePits.end(); it++){ Board tempBoard = _board; int pitNum = it->first; int score = analyzePlay(pitNum,tempBoard,possiblePits); } return 0; } int analyzePlay(int pitNum,Board tempBoard,std::map<int,int> &possibleMoves){ int tempBoardSize = tempBoard.getBoardSize(); int tempSeeds = tempBoard.getPitValue(pitNum); int lastPitSown; tempBoard.setPitToZero(pitNum); for(int index = 1; index <= tempSeeds; index++){ if(pitNum == tempBoardSize * 2 + 1){ //skips over human's score pit pitNum += 2; lastPitSown = pitNum; tempBoard.incrementPit(pitNum); } else{ pitNum++; lastPitSown = pitNum; tempBoard.incrementPit(pitNum); } } if(tempBoard.getPitValue(lastPitSown) == 1 && lastPitSown >= tempBoardSize + 2 && lastPitSown <= tempBoardSize * 2 + 1){ //turn ends. last seed sown into empty pit on opponent side. } else if(tempBoard.getPitValue(lastPitSown) > 1 && lastPitSown != tempBoardSize + 1){ //keep playing with next pit. last seed was sown into non-empty pit. } else if(lastPitSown == tempBoardSize + 1){ //extra turn. last seed sown into score pit. } else if(tempBoard.getPitValue(lastPitSown) == 1 && lastPitSown != tempBoardSize + 1 && lastPitSown <= tempBoardSize && lastPitSown >= 1 ){ //turn ends. last seed sown into empty pit on your side. capture. } return 0; } 我得到的错误: Error 1 error LNK2019: unresolved external symbol "public: int __thiscall ComputerPlayer::analyzePlay(int,class Board,class std::map<int,int,struct std::less<int>,class std::allocator<struct std::pair<int const,int> > > &)" (?analyzePlay@ComputerPlayer@@QAEHHVBoard@@AAV?$map@HHU?$less@H@std@@V?$allocator@U?$pair@$$CBHH@std@@@2@@std@@@Z) referenced in function "public: int __thiscallComputerPlayer::getBestMoves(void)" (?getBestMoves@ComputerPlayer@@QAEHXZ) C:UsersJoshDropboxCongkak_2Congkak_2ComputerPlayer.obj Error 2 error LNK1120: 1 unresolved externals C:UsersJoshDropboxCongkak_2DebugCongkak_2.exe 解决方法
两件事情:
>添加#include< map>在顶部,并使用std :: map而不是地图. 以下是两个方面的做法: #include<map> void function2(std::map<int,int> &temp_map); //forward declaration void function1(){ std::map<int,int> my_map; //automatic variable //no need to make it pointer! function2(my_map); } void function2(std::map<int,int> &temp_map){ //do stuff with the map } 还要注意,尽可能避免新的.默认使用自动变量,除非你有很强的理由不使用它. 自动变量很快,代码看起来整齐干净.使用它们,更容易编写异常安全的代码. 编辑: 现在当你发布错误,你也意识到,
正如您在评论中所说. 很好,你自己弄清楚了.但是,当您提出问题时,始终将错误发布在您的第一篇文章中.记住这一点. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |