Scope problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yeshello54
    New Member
    • Mar 2009
    • 54

    Scope problem

    Hey guys whats going on. I have a quick question about scope. I have three files. my opcodetab.h , opcodetab.cc and my driver.cpp. when i try to call my get_machine_cod e() function while passing the string "ADD" i get the following error. "get_machine_co de' was not declared in this scope".Im a little confused and lost right now .So any insight would be awesome. Thanks guys..my code is below...

    //////////opcodetab.h//////////////////////////////////
    #ifndef OPCODETAB_H
    #define OPCODETAB_H

    #include <iostream>
    #include <string>
    #include <map>
    using namespace std;
    class opcodetab {
    public:
    opcodetab();
    string get_machine_cod e(string);
    int get_instruction _size(string);
    private:
    typedef map<string, string> Type;
    Type Map;
    };
    #endif

    //////////////opcodetab.cc////////////////////////
    #include "opcodetab. h"
    #include <iostream>
    #include <string>
    #include <map>
    #include <utility>


    using namespace std;
    opcodetab::opco detab()

    {

    typedef map<string, string> Type;
    Type Map;
    //////////////////////opcodes//////////////////
    Map["ADD"]= "18";Map["ADDF"]= "58";Map["ADDR"]= "90";Map["AND"]= "40";
    Map["CLEAR"]= "B4";Map["COMP"]= "28";Map["COMPF"]= "88";Map["COMPR"]= "A0";
    Map["DIV"]= "24";Map["DIVF"]= "64";Map["DIVR"]= "9C";Map["FIX"]= "C4";
    Map["FLOAT"]= "CO";Map["HIO"]= "F4";Map["J"]= "3C";Map["JEQ"]= "30";
    Map["JGT"]= "34";Map["JLT"]= "38";Map["JSUB"]= "48";Map["LDA"]= "00";
    Map["LDB"]= "68";Map["LDCH"]= "50";Map["LDF"]= "70";Map["LDL"]= "08";
    Map["LDS"]= "6C";Map["LDT"]= "74";Map["LDX"]= "04";Map["LPS"]= "DO";
    Map["MULF"]= "60";Map["MULR"]= "98";Map["NORM"]= "C8";Map["OR"]= "44";
    Map["RD"]= "D8";Map["RMO"]= "AC";Map["RSUB"]= "4C";Map["SHIFTL"]= "A4";
    Map["SHIFTR"]= "A8";Map["SIO"]= "F0";Map["SSK"]= "EC";Map["STA"]= "0C";
    Map["STB"]= "78";Map["STCH"]= "54";Map["STF"]= "80";Map["STI"]= "D4";
    Map["STL"]= "14";Map["STS"]="7C";Map["STSW"]= "E8";Map["STT"]= "84";
    Map["STX"]= "10";Map["SUB"]= "1C";Map["SUBF"]= "5C";Map["SUBR"]= "94";
    Map["SVC"]= "B0";Map["TD"]= "E0";Map["TIO"]= "F8";Map["TIX"]= "2C";
    Map["TIXR"]= "B8";Map["WD"]= "DC";


    }

    string opcodetab::get_ machine_code(st ring )
    {

    Type::iterator iter = Map.begin(); Map.erase(iter) ;
    string c;

    cin >> c;

    iter = Map.find(c);
    if( iter != Map.end() )
    cout << "OPTCODE is: " << iter->second << endl;
    else
    cout << "No such OPTCODE value" << endl;

    Map.clear();

    return 0;
    }
    ////////////////////////////////driver.cpp////////////////////////
    #include <iostream>
    #include <map>
    #include <utility>
    #include <string>
    #include <map>
    #include "opcodetab. h"
    using namespace std;

    int main()
    {

    opcodetab();
    get_machine_cod e("ADD");

    }
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Because the function is declared within a class I think you have to call it with class_name::fun ction_name.

    Comment

    • yeshello54
      New Member
      • Mar 2009
      • 54

      #3
      I tried that and now i get an error : "cannot call member function 'std::string opcodetab::get_ machine_code(st d::string)' without object" ...i dont understand because i thought passing my "ADD" was the object

      Comment

      • Meetee
        Recognized Expert Contributor
        • Dec 2006
        • 928

        #4
        But where ADD is created that you are passing??

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by yeshello54
          I tried that and now i get an error : "cannot call member function 'std::string opcodetab::get_ machine_code(st d::string)' without object" ...i dont understand because i thought passing my "ADD" was the object
          You defined that Map variable as a member of an object of that class. The function is also defined as a member function so you need an object of that particular class. Note that every object of that obcodetab has such a Map member variable; I doubt that you want to have all those duplicate Maps around; I'd make that Map table static.

          In order to call that function you either need to have an object (instantiation) of that class or you can make it a static (class) function because it doesn't really need a particular object, just the Map variable which is static.

          kind regards,

          Jos

          Comment

          • yeshello54
            New Member
            • Mar 2009
            • 54

            #6
            alright cool. Thanks for the advice..i will see what i can do

            Comment

            • yeshello54
              New Member
              • Mar 2009
              • 54

              #7
              So i was trying to play around and i have come to another problem. In my class decleration i went ahead and declared my map container static. so it looked like "static typedef map<string, string> mapType; mapType myMap;" that seemed to fix my compile error but when i compile and run i get a segmentation fault. i was using the debugger and it seg faults in my get_machine_cod e method call right before the line " cin>> c; " and before "iter =myMap.find(c); " so if anyone could point out whats wrong that would be cool..here is my updated code...thanks.. .
              ///////////////// my .cc file/////////
              #include "opcodetab. h"
              #include <iostream>
              #include <string>
              #include <map>
              #include <utility>


              using namespace std;
              opcodetab::opco detab()

              {
              /* typedef map<string, string> mapType;
              mapType myMap;*/


              //////////////////////opcodes//////////////////
              myMap["ADD"]= "18";myMap["ADDF"]= "58";myMap["ADDR"]= "90";myMap["AND"]= "40";
              myMap["CLEAR"]= "B4";myMap["COMP"]= "28";myMap["COMPF"]= "88";myMap["COMPR"]= "A0";
              myMap["DIV"]= "24";myMap["DIVF"]= "64";myMap["DIVR"]= "9C";myMap["FIX"]= "C4";
              myMap["FLOAT"]= "CO";myMap["HIO"]= "F4";myMap["J"]= "3C";myMap["JEQ"]= "30";
              myMap["JGT"]= "34";myMap["JLT"]= "38";myMap["JSUB"]= "48";myMap["LDA"]= "00";
              myMap["LDB"]= "68";myMap["LDCH"]= "50";myMap["LDF"]= "70";myMap["LDL"]= "08";
              myMap["LDS"]= "6C";myMap["LDT"]= "74";myMap["LDX"]= "04";myMap["LPS"]= "DO";
              myMap["MULF"]= "60";myMap["MULR"]= "98";myMap["NORM"]= "C8";myMap["OR"]= "44";
              myMap["RD"]= "D8";myMap["RMO"]= "AC";myMap["RSUB"]= "4C";myMap["SHIFTL"]= "A4";
              myMap["SHIFTR"]= "A8";myMap["SIO"]= "F0";myMap["SSK"]= "EC";myMap["STA"]= "0C";
              myMap["STB"]= "78";myMap["STCH"]= "54";myMap["STF"]= "80";myMap["STI"]= "D4";
              myMap["STL"]= "14";myMap["STS"]="7C";myMap["STSW"]= "E8";myMap["STT"]= "84";
              myMap["STX"]= "10";myMap["SUB"]= "1C";myMap["SUBF"]= "5C";myMap["SUBR"]= "94";
              myMap["SVC"]= "B0";myMap["TD"]= "E0";myMap["TIO"]= "F8";myMap["TIX"]= "2C";
              myMap["TIXR"]= "B8";myMap["WD"]= "DC";
              }


              string opcodetab::get_ machine_code(st ring)
              {
              mapType::iterat or iter = myMap.begin();
              //myMap.erase(ite r);
              string c;
              cin >> c;

              iter =myMap.find(c);

              if( iter != myMap.end() )
              cout << "OPCODE: " << iter->second << endl;
              else
              cout << "OPDOCDE NOT FOUND" << endl;

              // clear the entries in the map
              myMap.clear();
              return 0;
              }


              /////////////my .h file//////////
              #ifndef OPCODETAB_H
              #define OPCODETAB_H

              #include <iostream>
              #include <string>
              #include <map>

              using namespace std;

              class opcodetab {
              public:
              opcodetab();
              string get_machine_cod e(string);
              int get_instruction _size(string);

              private:

              static typedef map<string, string> mapType;
              mapType myMap;
              static typedef map<string, string> mapType;
              mapType size;
              //static string c;

              };

              #endif

              //////////////my driver.cpp//////////
              #include <iostream>
              #include <map>
              #include <utility>
              #include <string>
              #include <map>
              #include "opcodetab. h"
              using namespace std;

              int main()
              {
              opcodetab();

              opcodetab::get_ machine_code("A DDF");

              }

              Comment

              • yeshello54
                New Member
                • Mar 2009
                • 54

                #8
                ops..nevermind i got it to work..thanks for the help guys

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by yeshello54
                  ops..nevermind i got it to work..thanks for the help guys
                  So how did you solve it? By making everything static?

                  kind regards,

                  Jos

                  Comment

                  • yeshello54
                    New Member
                    • Mar 2009
                    • 54

                    #10
                    I declared my map container to be static in the private section of my class decleration. then I just fixed a couple of small things and it worked. Thanks for the pointers.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by yeshello54
                      I declared my map container to be static in the private section of my class decleration. then I just fixed a couple of small things and it worked. Thanks for the pointers.
                      You do realize that your class can only handle one type of CPU now do you? Another approach would've been one class instantiation per CPU type; a factory method could've given you the correct instance given an ID for a CPU, but maybe you don't need that additional complexity.

                      kind regards,

                      Jos

                      Comment

                      • yeshello54
                        New Member
                        • Mar 2009
                        • 54

                        #12
                        im not sure i really understand that. But its only a school project so i dont need it to run on different types of cpu's just mine.

                        Comment

                        Working...