c++通过引用将映射传递给函数

发布于 2024-12-10 03:10:59 字数 3478 浏览 1 评论 0原文

如何通过引用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋叶绚丽 2024-12-17 03:10:59

有两件事:

  • 在顶部添加 #include

    ,并使用 std::map 而不仅仅是 map

  • function1之上定义function2,或者至少在function1之上声明function2

以下是两者应该如何完成的:

#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。默认情况下使用自动变量,除非您有充分的理由不使用它。

自动变量速度很快,而且代码看起来整洁干净。有了它们,就可以更轻松地编写异常安全的代码。

编辑:

现在当您发布错误时,您还意识到,

我忘记将该函数所属的类添加到它的开头。如:Player::function2(std::map&temp_map){}

,正如您在评论中所说。

很好,你自己弄清楚了。但是,当您提出问题时,请务必在您的第一篇文章中发布错误。记住这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文