转换c++中enum变量中的字符串变量
Convert a string variable in enum variable in c++
我需要你的帮助,请特别知道我可以在枚举变量转换字符串变量。
下面是我的代码: deco_fill.h
#include <iostream>
using namespace std;
#include <string.h>
class A{
class B{
public:
enum tStrict{
"ST_UNKNOWN"=-1;
"ST_USE"=0;
"ST_DEL"=1;
}
public:
tStrict mType;
void setStrict(tStrict& newStrict ){
return mType=newStrict;
}
}
}
test.h
#include <iostream>
using namespace std;
#include <string.h>
#include <deco_fill.h>
class C
{
public:
A::B::tStrict CnvStrToEnum(const string& str); //This method will return a tStrict type
}
test.cpp
#include <iostream>
using namespace std;
#include <string.h>
#include <test.h>
#include <deco_fill.h>
A::B::tStrict C::CnvStrToEnum(const string& str)
{
if (str=="ST_USE")
return ST_USE;
else if (str=="ST_DEL")
return ST_DEL;
else
return ST_UNKNOWN;
}
test_set.cpp
#include <iostream>
using namespace std;
#include <string.h>
#include <deco_fill.h>
#include <test.h>
string st=ST_USE;
A::B::tStrict strictType=CnvStrToEnum(st);
setStrict(strictType);//I want here to use the setStrict methode to set a new variable of type enum with that. This part is not important
我在test.cpp中有一个编译错误,如ST_DEL
, ST_USE
和ST_UNKNOWN
没有声明。我在这里需要什么,以及如何在枚举类型中正确地使用字符串类型。谢谢你的帮助。
enum
是数字常量(不是字符串),所以你不能写
enum tStrict{
"ST_UNKNOWN"=-1;
"ST_USE"=0;
"ST_DEL"=1;
}
还要注意每个枚举常量后面的逗号(不是分号)。
所以你应该写:
enum tStrict{
ST_UNKNOWN=-1,
ST_USE,
ST_DEL
};
通常可以将枚举常量转换为对应的字符串:
const char *tStrictStr( const enum tStrict t )
{
switch( t )
{
case ST_UNKNOWN : return "ST_UNKNOWN" ;
case ST_USE : return "ST_USE" ;
case ST_DEL : return "ST_DEL" ;
default : return "ST_UNKNOWN" ;
}
}
相关文章:
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 从字符串变量中逐字符读取单词
- 对函数 PlaySound 使用字符串变量
- C++中的字符串变量
- 如何在 c++ 中将字符串迭代器变量传递给函数?
- 如何使用C++将字符串中的字符转换为整数变量
- 字符串变量,比如说"字符串str",可以直接复制到数组中吗?
- 我可以用字符串变量而不是它的名字创建容器吗? C++
- 字符串变量的大小
- 使变量字符串忽略转义序列
- 如何在 C 宏中将变量字符串与文字字符串连接起来?
- 在 Py_BuildValue 中返回变量字符串
- 如何将字符串作为变量与另一个变量字符串进行比较
- 从变量字符串中提升文件系统路径
- 用变量字符串声明设置容器
- 通过变量(字符串)调用obejct和方法
- C++创建文本和变量字符串
- 如何在循环中使用变量字符串