0

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

Naveed
  • 41,517
  • 32
  • 98
  • 131
  • 9
    You should start with [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list). – James McNellis Jan 01 '11 at 04:39

4 Answers4

2

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 -

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

Hope this helps !

Mahesh
  • 34,573
  • 20
  • 89
  • 115
1

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;
}
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
0

Take a look at boost assign map_list_of here. It can be used to assign map. Then use find method from the map (the reference is here)

BЈовић
  • 62,405
  • 41
  • 173
  • 273
0

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.

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;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055