c++通过引用将映射传递给函数
c++ pass a map by reference into function
如何将map
通过reference
传递到函数中?Visual Studio 2010给了我一个unresolved externals
错误。目前,我有以下简化的代码:
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
而不是map
。 - 将
function2
定义在function1
之上,或者至少将function2
声明在function1
之上。
以下是如何做到这两点:
#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
}
还要注意尽可能避免使用new
。默认情况下使用自动变量,除非您有非常强烈的理由不使用它。
自动变量很快,代码看起来整洁干净。有了它们,编写异常安全的代码就容易多了。
编辑:现在当你发布错误时,你也意识到,
我忘记将函数所属的Class添加到它的开头。如:Player::function2(std::map<int,> &temp_map){}
,正如你在评论中所说的。
很好,你自己想出来了。但是,当你问问题时,总是在你的第一个帖子中发布错误。记住这一点。
相关文章:
- 使用std::函数映射对象方法
- 删除 std::函数映射
- 具有单个参数的布尔函数映射
- 如何使用成员函数映射参数包?(非恢复性,折叠前表达式)
- 如何在类中存储指向字符串到成员函数映射的智能指针,以便它不会因类型不完整而失败?
- 将函数映射提升为字符串
- 将C++成员函数映射到C回调
- 如何使用JNA将返回字符串数组(constchar**)的函数映射到Java
- 如何将可变数量的不同类型的参数传递给 C++11 函数映射中的函数
- 将 std 绑定传递到函数映射的问题
- 异构类型的成员函数映射
- c++的cmath函数映射
- 函数映射容器中C++函数返回类型重载:自动推导失败,并出现编译器错误
- c++构造函数映射对象(已编辑)
- QtConcurrency::与成员函数映射
- c++函数映射实现
- 使用boost的字符串和函数映射会导致编译错误
- 访问与其他函数映射的文件
- 将C++重载函数映射到异构元组上
- 用于成员函数映射的c++find()