gpt4 book ai didi

c++ - 如何从函数返回值

转载 作者:行者123 更新时间:2023-11-28 01:08:53 25 4
gpt4 key购买 nike

我使用一个函数来计算我在映射中初始化的某些指令的信息,就像这样

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

如果我在函数中 cout y 给我一个很好的答案,这就是我需要的但是从函数返回值以便我可以在函数外使用它有一个问题,它给出了一个完全不同的答案,问题是什么

最佳答案

你在函数中的第二个参数是一个常量引用。尝试更换 -

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

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

并在您的 if 条件中,删除 y 的新声明并将其替换为 -

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

希望对您有所帮助!

关于c++ - 如何从函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4573177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com