簡體   English   中英

如何從函數返回值

[英]How can I return a value from a function

我使用了一個函數來計算有關我在地圖中初始化的某些指令的信息,像這樣

void get_objectcode(char*&token1,const int &y)
{
map<string,int> operations;
    operations["ADD"] = 18;
    operations["AND"] = 40;
    operations["COMP"] = 28;
    operations["DIV"] = 24;
    operations["J"] = 0X3c;
    operations["JEQ"] =30;
    operations["JGT"] =34;
    operations["JLT"] =38;
    operations["JSUB"] =48;
    operations["LDA"] =00;
    operations["LDCH"] =50;
    operations["LDL"] =55;
    operations["LDX"] =04;
    operations["MUL"] =20;
    operations["OR"] =44;
    operations["RD"] =0xd8;
    operations["RSUB"] =0x4c;
    operations["STA"] =0x0c;
    operations["STCH"] =54;
    operations["STL"] =14;
    operations["STSW"] =0xe8;
    operations["STX"] =10;
    operations["SUB"] =0x1c;
    operations["TD"] =0xe0;
    operations["TIX"] =0x2c;
    operations["WD"] =0xdc;

         if  ((operations.find("ADD")->first==token1)||(operations.find("AND")->first==token1)||(operations.find("COMP")->first==token1)
            ||(operations.find("DIV")->first==token1)||(operations.find("J")->first==token1)||(operations.find("JEQ")->first==token1)
            ||(operations.find("JGT")->first==token1)||(operations.find("JLT")->first==token1)||(operations.find("JSUB")->first==token1)
            ||(operations.find("LDA")->first==token1)||(operations.find("LDCH")->first==token1)||(operations.find("LDL")->first==token1)
            ||(operations.find("LDX")->first==token1)||(operations.find("MUL")->first==token1)||(operations.find("OR")->first==token1)
            ||(operations.find("RD")->first==token1)||(operations.find("RSUB")->first==token1)||(operations.find("STA")->first==token1)||(operations.find("STCH")->first==token1)||(operations.find("STCH")->first==token1)||(operations.find("STL")->first==token1)
            ||(operations.find("STSW")->first==token1)||(operations.find("STX")->first==token1)||(operations.find("SUB")->first==token1)
            ||(operations.find("TD")->first==token1)||(operations.find("TIX")->first==token1)||(operations.find("WD")->first==token1))

            {
                int y=operations.find(token1)->second;
                //cout<<hex<<y<<endl;
            }

        return ;
}

如果我在函數中使用y可以給我一個很好的答案,這正是我所需要的,但是在返回函數中的值時出現問題,以便我可以在函數外使用它,它給出了一個完全不同的答案,是問題

函數中的第二個參數是常量引用。 嘗試更換-

void get_objectcode(char*&token1,const int &y) 

void get_objectcode(char*&token1,int &y) 

並在您的if條件中,刪除y的新聲明並將其替換為-

y=operations.find(token1)->second;

希望這可以幫助 !

這可能更接近您想要的:

void get_objectcode(const char *token, int &y)
{
    typedef std::map<std::string,int> OpMap;
    OpMap operations;
    operations["ADD" ] = 18;
    operations["AND" ] = 40;
    operations["COMP"] = 28;
    operations["DIV" ] = 24;
    // etc.
    operations["SUB" ] = 0x1c;
    operations["TD"  ] = 0xe0;
    operations["TIX" ] = 0x2c;
    operations["WD"  ] = 0xdc;

    OpMap::iterator result = operations.find(token);

    // note: assigns 0 to y if token is not found
    y = (result == operations.end()) ? 0 : result->second;

    //std::cout << std::hex << y << std::endl;
}

看看這里 boost boost map_list_of。 可用於分配地圖。 然后使用地圖中的find方法(參考位於此處

您的地圖僅存在於該函數中。 因此,元素僅存在於該功能內。 如果在調用該函數的位置使用y初始化引用,則該引用將是對將不再存在的元素的引用。

您不應該真正在每次調用該函數時都創建映射,至少最好僅從該函數正常返回值:

std::map<std::string,int> operations;
operations["ADD"]  = 18;
operations["AND"]  = 40;
operations["COMP"] = 28;
operations["DIV"]  = 24;
operations["J"]    = 0X3c;
operations["JEQ"]  = 30;
operations["JGT"]  = 34;
operations["JLT"]  = 38;
operations["JSUB"] = 48;
operations["LDA"]  = 00;
operations["LDCH"] = 50;
operations["LDL"]  = 55;
operations["LDX"]  = 04;
operations["MUL"]  = 20;
operations["OR"]   = 44;
operations["RD"]   = 0xd8;
operations["RSUB"] = 0x4c;
operations["STA"]  = 0x0c;
operations["STCH"] = 54;
operations["STL"]  = 14;
operations["STSW"] = 0xe8;
operations["STX"]  = 10;
operations["SUB"]  = 0x1c;
operations["TD"]   = 0xe0;
operations["TIX"]  = 0x2c;
operations["WD"]   = 0xdc;

int get_objectcode(const std::string& key)
{
    std::map<std::string, int>::iterator it = operations.find(key);
    if (it == operations.end())
        return -1;
    else
        return it->second;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM