C++ 在头文件中声明时未在范围中声明?
我想在头文件中声明在main方法中全局使用的函数、向量和结构 sicxe_asm.h如下所示:C++ 在头文件中声明时未在范围中声明?,c++,scope,header-files,C++,Scope,Header Files,我想在头文件中声明在main方法中全局使用的函数、向量和结构 sicxe_asm.h如下所示: #ifndef SICXE_ASM_H #define SICXE_ASM_H using namespace std; #include <string> #include <sstream> #include <vector> class sicxe_asm { public: private: string filename; //
#ifndef SICXE_ASM_H
#define SICXE_ASM_H
using namespace std;
#include <string>
#include <sstream>
#include <vector>
class sicxe_asm
{
public:
private:
string filename; // file to be assembled
string validate_hex_address(string str);
string decimal_to_hex(int dec);
int hex_to_decimal(string hexvalue);
bool is_blank_or_comment(vector<string> command);
bool is_decimal(string tempStr);
struct listingFileLine
{
string address;
string label;
string opcode;
string operand;
listingFileLine() : address(""), label(""), opcode(""), operand("") {}
};
vector <listingFileLine> listingFileVec;
};
#endif
string validate_hex_address(string str)
{
if(str.at(0) != '$')
{
throw driver_exception("Invalid START address");
}
str.erase(0,1); // remove the $
return str;
}
string decimal_to_hex(int dec)
{
stringstream ss;
ss << hex << dec;
string hexvalue = ss.str();
return hexvalue;
}
int hex_to_decimal(string hexvalue)
{
stringstream ss;
int decimalvalue;
ss << hexvalue;
ss >> hex >> decimalvalue;
return decimalvalue;
}
bool is_blank_or_comment(vector<string> command)
{
if(command[LABEL] == "" && command[OPCODE] == "" && command[OPERAND] == "")
return true;
return false;
}
bool is_decimal(string tempStr)
{
const char * str = tempStr.c_str();
for(unsigned int i = 0; i <= strlen(str)-1; i++)
{
if(!isdigit(str[i]))
return false;
}
return true;
}
\ifndef SICXE\u ASM\H
#定义SICXE\u ASM\u H
使用名称空间std;
#包括
#包括
#包括
类sicxe_asm
{
公众:
私人:
字符串文件名;//要组装的文件
字符串验证十六进制地址(字符串str);
字符串十进制到十六进制(int-dec);
整数十六进制到十进制(字符串十六进制值);
bool是_blank_或_comment(矢量命令);
bool是_十进制(字符串tempStr);
结构listingFileLine
{
字符串地址;
字符串标签;
字符串操作码;
字符串操作数;
listingFileLine():地址(“”)、标签(“”)、操作码(“”)、操作数(“”{}
};
向量列表文件向量;
};
#恩迪夫
在sicxe_asm.cpp中,我这样使用它们:
#ifndef SICXE_ASM_H
#define SICXE_ASM_H
using namespace std;
#include <string>
#include <sstream>
#include <vector>
class sicxe_asm
{
public:
private:
string filename; // file to be assembled
string validate_hex_address(string str);
string decimal_to_hex(int dec);
int hex_to_decimal(string hexvalue);
bool is_blank_or_comment(vector<string> command);
bool is_decimal(string tempStr);
struct listingFileLine
{
string address;
string label;
string opcode;
string operand;
listingFileLine() : address(""), label(""), opcode(""), operand("") {}
};
vector <listingFileLine> listingFileVec;
};
#endif
string validate_hex_address(string str)
{
if(str.at(0) != '$')
{
throw driver_exception("Invalid START address");
}
str.erase(0,1); // remove the $
return str;
}
string decimal_to_hex(int dec)
{
stringstream ss;
ss << hex << dec;
string hexvalue = ss.str();
return hexvalue;
}
int hex_to_decimal(string hexvalue)
{
stringstream ss;
int decimalvalue;
ss << hexvalue;
ss >> hex >> decimalvalue;
return decimalvalue;
}
bool is_blank_or_comment(vector<string> command)
{
if(command[LABEL] == "" && command[OPCODE] == "" && command[OPERAND] == "")
return true;
return false;
}
bool is_decimal(string tempStr)
{
const char * str = tempStr.c_str();
for(unsigned int i = 0; i <= strlen(str)-1; i++)
{
if(!isdigit(str[i]))
return false;
}
return true;
}
string验证十六进制地址(string str)
{
如果(str.at(0)!=“$”)
{
抛出驱动程序_异常(“无效的起始地址”);
}
str.erase(0,1);//删除$
返回str;
}
字符串十进制到十六进制(整数十进制)
{
细流ss;
ss>小数;
返回小数;
}
bool为空白或注释(矢量命令)
{
if(命令[标签]==“”&命令[操作码]==“”&命令[操作数]==“”)
返回true;
返回false;
}
布尔为十进制(字符串tempStr)
{
const char*str=tempStr.c_str();
对于(unsigned int i=0;i首先,方法在私有部分,因此不能直接调用它们。如果要在类声明之外调用它们,请将它们放在公共部分
其次,.cpp文件中方法的定义(实现)必须使用:
,例如:
bool sicxe_asm::is_decimal(string tempStr) { ... }
^^^^^^^^^^^
当除了默认构造函数外,赋值都是私有的时,sicxe\u asm
的意义是什么?只是为了在.cpp文件中设置全局使用的函数、对象和结构“在.cpp文件中设置全局使用”Uhmm,什么??private
意味着私有!!你是指未命名的命名空间、编译单元私有吗e???@user3339703-他们不会(请阅读字典中“private”一词)你能更具体地回答你的问题吗?我认为问题是你需要将函数定义为类的一部分,例如,string sicxe\u asm::validate\u hex\u address
等等。当我公开这些函数并在其上添加范围解析时,这两个函数都不能解决问题。仍然需要转发引用