Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C++;? 我有一个C++ 测试.HH< /Cord>头文件,在其中我声明了这个枚举-< /p> enum TestTypeOpt {TestingOne, TestingTwo};_C++_Enums - Fatal编程技术网

如何在C++;? 我有一个C++ 测试.HH< /Cord>头文件,在其中我声明了这个枚举-< /p> enum TestTypeOpt {TestingOne, TestingTwo};

如何在C++;? 我有一个C++ 测试.HH< /Cord>头文件,在其中我声明了这个枚举-< /p> enum TestTypeOpt {TestingOne, TestingTwo};,c++,enums,C++,Enums,现在,在我的Testing.cc类中,我试图从枚举中打印出TestingOne和TestingTwo字符串,如下所示 cout << "From command" << Testing::TestingOne << endl; cout << "From command" << Testing::TestingTwo << endl; cout如果有更好的方法,请有人纠正我,但我知道的方法是使用宏 只需键入#defin

现在,在我的
Testing.cc
类中,我试图从枚举中打印出
TestingOne
TestingTwo
字符串,如下所示

 cout << "From command" << Testing::TestingOne << endl;
 cout << "From command" << Testing::TestingTwo << endl;

cout如果有更好的方法,请有人纠正我,但我知道的方法是使用宏

只需键入
#define str(x)#x
这将用代码中写入的名称替换x

然后使用以下示例:
cout@arshan读了这篇文章,他说了他想做的事情。当然你可以这么做,但是如果你使用的是变量,这是毫无意义的。请参阅我对答案的编辑。@Qix他没有要求变量。他给出了一个明确的例子,并询问如何打印出姓名。我觉得这正是他的问题的答案。如果是这样的话,就用一个字符串。为什么要使用宏?@Qix出于某种原因,他试图打印出
enum
的名称。就这样。如果将其更改为字符串,它将不再与枚举/变量耦合。使用它对stdout调试非常有帮助,因为您可以立即打印出变量名。您假设他想将枚举变量的实际值打印为字符串,但这不是他的问题。所以我没有得到-1…@Qix