2011-10-14 26 views
10

mapreferenceで関数に渡すにはどうすればよいですか? Visual Studio 2010でエラーunresolved externalsが表示されます。現在、私は以下の単純化されたコードがあります。C++参考にマップを関数に渡す

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> > > &)" ([email protected]@@[email protected]@[email protected][email protected]@[email protected]@[email protected][email protected][email protected]@@@[email protected]@[email protected]@@Z) referenced in function "public: int __thiscallComputerPlayer::getBestMoves(void)" ([email protected]@@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 
+0

未解決の外部エラーはリンクエラーを意味します。私はそれが関数への参照によってマップを渡すことと何ら関係がないと考えています - それは構文エラーである可能性がより高くなります。 – Ayjay

+0

また、このコードは実行すると機能します。おそらく、あなたのプロジェクト設定が正しく設定されていないでしょう。 – Ayjay

答えて

23

2つのこと:

  • は、上部に#include<map>を追加して、ちょうどmapの代わりにstd::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を避けるために注意してください。デフォルトでは自動の変数を使用します。ただし、使用しない理由が非常に強い場合を除きます。

自動変数は高速で、コードはすっきりとしてきれいに見えます。それらを使用すると、例外セーフなコードを書く方が簡単です。

EDIT:あなたがエラーを掲示よう

は今、あなたはまた、私は関数はそれの最初の部分だったクラスを追加するのを忘れ、という

実現しました。以下のように:プレーヤー::機能2(> & temp_mapのstd ::マップ<のint、int型){}

、あなたはコメントで言ったように。

あなたは自分でそれを理解して良かったです。しかし、依然として、あなたが質問をするとき、あなたの最初の投稿に常にエラーを投稿してください。これを覚えて。

+0

function2の上にfunction2を置くこともできます... – Ayjay

+0

さて、私は私が必要とするものを変更したと確信していますが、まだ解決されていない外部エラーが発生しています。自分のプログラムで実際に使っているコードで元の投稿を1分間で編集します。 – Cuthbert

+1

@ d2jxp:もっと多くのコードと一緒にエラーを投稿してみませんか?どのようにして正確にエラーが発生したのか、どうすればわかるのでしょうか? – Nawaz

関連する問題