范围问题 [英] Scope problem

查看:78
本文介绍了范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,伙计们有什么事了。我有一个关于范围的快速问题。我有三个文件。我的opcodetab.h,opcodetab.cc和我的driver.cpp。当我尝试在传递字符串ADD时调用我的get_machine_code()函数我收到以下错误。 get_machine_code未在此范围内声明。我现在有点困惑和迷失。所以任何见解都会很棒。谢谢大家..我的代码在下面...


//////////opcodetab.h ////////////// ////////////////////

#ifndef OPCODETAB_H

#define OPCODETAB_H


#include< iostream>

#include< string>

#include< map>

using namespace std;

class opcodetab {

public:

opcodetab();

string get_machine_code(string);

int get_instruction_size(string);

private:

typedef map< string,string>输入;

类型地图;

};

#endif


///// /////////opcodetab.cc////////////////////////

#include" opcodetab。 h"

#include< iostream>

#include< string>

#include< map>

#include< utility>


使用命名空间std;

opcodetab :: opcodetab()


{


typedef map< string,string>输入;

类型地图;

//////////////////////操作码////// ////////////

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英寸;地图[" 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(string)

{


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

string c;


cin>> c;


iter = Map.find(c);

if(iter!= Map.end())

cout<< OPTCODE是: << iter-> second<< endl;

else

cout<< 没有这样的OPTCODE值 <<结束;


Map.clear();


返回0;

}

////////////////////////////////driver.cpp///////////// ///////////

#include< iostream>

#include< map>

#include< ;实用>

#include< string>

#include< map>

#include" opcodetab.h"

using namespace std;


int main()

{


opcodetab();

get_machine_code(" ADD");


}

解决方案

因为函数是在a class我认为你必须用class_name :: function_name调用它。


我试过了,现在我得到一个错误:不能调用成员函数''std :: string opcodetab :: get_machine_code(std :: string)''没有对象' ...我不明白,因为我认为通过我的ADD是对象


但是你传递了ADD的地方?


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_code() function while passing the string "ADD" i get the following error. "get_machine_code'' 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_code(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::opcodetab()

{

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(string )
{

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_code("ADD");

}

解决方案

Because the function is declared within a class I think you have to call it with class_name::function_name.


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


But where ADD is created that you are passing??


这篇关于范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆