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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7763397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:27:36  来源:igfitidea点击:

c++ pass a map by reference into function

c++mappass-by-reference

提问by Cuthbert

How can I pass a mapby referenceinto a function? Visual Studio 2010 is giving me an unresolved externalserror. Currently, I have the following simplified code:

我如何传递一个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
}

There's a few answers to similar questions here, but they make use of typedefand adding std::to the beginning of the definition but I'm really not sure why.

这里有一些类似问题的答案,但它们使用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;
}

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

回答by Nawaz

Two things:

两件事情:

  • Add #include<map>at the top, and use std::mapinstead of just map.
  • Define function2above function1Or at least declare function2above function1.
  • 添加#include<map>在顶部,并使用std::map而不仅仅是map
  • 定义function2以上function1或者至少声明function2以上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 newas much as possible. Use automaticvariables by default, unless you've very strong reason not to use it.

还要注意尽量避免new。默认情况下使用自动变量,除非您有非常充分的理由不使用它。

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){}

我忘了将函数所属的类添加到它的开头。如: 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.

很好,你自己想出来了。但是,当您提出问题时,请始终在您的第一篇文章中发布错误。记住这一点。