c++通过引用将映射传递给函数
如何通过引用
将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:\Users\Josh\Dropbox\Congkak_2\Congkak_2\ComputerPlayer.obj
Error 2 error LNK1120: 1 unresolved externals C:\Users\Josh\Dropbox\Congkak_2\Debug\Congkak_2.exe
How can I pass a map
by reference
into a function? Visual Studio 2010 is giving me an unresolved externals
error. Currently, I have the following simplified code:
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
}
There's a few answers to similar questions here, but they make use of typedef
and adding std::
to the beginning of the definition but I'm really not sure why.
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;
}
The error I was getting:
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:\Users\Josh\Dropbox\Congkak_2\Congkak_2\ComputerPlayer.obj
Error 2 error LNK1120: 1 unresolved externals C:\Users\Josh\Dropbox\Congkak_2\Debug\Congkak_2.exe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两件事:
#include
,并使用
std::map
而不仅仅是map
。function1
之上定义function2
,或者至少在function1
之上声明function2
。以下是两者应该如何完成的:
另请注意,尽可能避免
new
。默认情况下使用自动变量,除非您有充分的理由不使用它。自动变量速度很快,而且代码看起来整洁干净。有了它们,就可以更轻松地编写异常安全的代码。
编辑:
现在当您发布错误时,您还意识到,
,正如您在评论中所说。
很好,你自己弄清楚了。但是,当您提出问题时,请务必在您的第一篇文章中发布错误。记住这一点。
Two things:
#include<map>
at the top, and usestd::map
instead of justmap
.function2
abovefunction1
Or at least declarefunction2
abovefunction1
.Here is how both should be done:
Also note that avoid
new
as much as possible. Use automatic variables by default, unless you've very strong reason not to use it.Automatic variables are fast, and the code looks neat and clean. With them it is easier to write exception-safe code.
EDIT:
Now as you posted the error, you also realized that,
, as you said in the comment.
Good that you figured it out yourself. But still, always post the error in your very first post, when you ask the question. Remember this.