简体   繁体   English

如何从函数返回值

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

I used a function to calculate information about certain instructions I intialized in a map,like this 我使用了一个函数来计算有关我在地图中初始化的某些指令的信息,像这样

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

which if I cout y in the function gives me an answer just fine which is what i need but there is a problem tring to return the value from the function so that I could use it outside the function , it gives a whole different answer, what is the problem 如果我在函数中使用y可以给我一个很好的答案,这正是我所需要的,但是在返回函数中的值时出现问题,以便我可以在函数外使用它,它给出了一个完全不同的答案,是问题

Yours second argument in the function is a constant reference. 函数中的第二个参数是常量引用。 Try replacing - 尝试更换-

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

with

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

and in your if condition, remove the new declaration of y and replace it with - 并在您的if条件中,删除y的新声明并将其替换为-

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

Hope this helps ! 希望这可以帮助 !

This is probably closer to what you want: 这可能更接近您想要的:

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

Take a look at boost assign map_list_of here . 看看这里 boost boost map_list_of。 It can be used to assign map. 可用于分配地图。 Then use find method from the map (the reference is here ) 然后使用地图中的find方法(参考位于此处

Your map exists only within that function. 您的地图仅存在于该函数中。 Therefore, the elements only exist within that function. 因此,元素仅存在于该功能内。 If, at the place where you call the function, you're initialising a reference with y , then that will be a reference to an element that will cease to exist. 如果在调用该函数的位置使用y初始化引用,则该引用将是对将不再存在的元素的引用。

You should not really be creating the map every time the function is called, and at least it would be preferable to just return the value normally from the function: 您不应该真正在每次调用该函数时都创建映射,至少最好仅从该函数正常返回值:

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