Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ pass a map by reference into function

Tags:

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 
like image 360
Cuthbert Avatar asked Oct 14 '11 05:10

Cuthbert


People also ask

How do you pass by reference in a function?

Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in. The following example shows how arguments are passed by reference.

Can we pass map as reference in C++?

No. But note that this question is unrelated to your code, as your code does not in fact pass map value as a reference.


1 Answers

Two things:

  • Add #include<map> at the top, and use std::map instead of just map.
  • Define function2 above function1 Or at least declare function2 above function1.

Here is how both should be done:

#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 } 

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,

I forgot to add the Class that the function was part of to the beginning of it. as in: Player::function2(std::map<int, int> &temp_map){}

, 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.

like image 94
Nawaz Avatar answered Oct 06 '22 10:10

Nawaz