我正在研究c ++,以下是我的模板类:
namespace My{
template<class config>
class BasicValue
{
public:
enum Type
{
NULL_TYPE = 0,
OBJECT_TYPE,
ARRAY_TYPE,
STRING_TYPE,
BOOL_TYPE,
INT_TYPE,
REAL_TYPE,
}; // Type
};// BasicValue
}// My
现在,我从另一个.cpp文件访问这个“Type”枚举。我得到所有枚举类型的编译错误:
error: ‘STRING_TYPE’ was not declared in this scope
如何在“我的”命名空间之外使用此枚举?
以下是代码用法,我使用switch case中的枚举值:
void printValue(const Value& val, int space)
{
int sp = space;
switch(val.type())
{
case STRING_TYPE:
break;
case BOOL_TYPE:
break;
case INT_TYPE:
break;
case REAL_TYPE:
break;
default:
exit(-1);
}
}
答案 0 :(得分:2)
像
这样的东西My::BasicValue<int>::Type t; // get an instance of the type
....
t = My::BasicValue<int>::STRING_TYPE; // get a value
但这假设您修复了代码中的所有语法错误,如in this example ..