我写了一个简单的例子来解决我在写他的程序时遇到的问题。
\n\n在程序执行期间,当从函数返回值时,我得到 input1 和 input2 的值,这些值永远不会改变。然后稍后,在程序过程中进行各种计算后,我得到一个结果,该结果也不再可变。
\n\n我正在尝试使用 switch-case 来比较它们,但出现一个错误“\xe2\x80\x98input1\xe2\x80\x99 的值在常量表达式中不可用”。
\n\n#include <iostream>\n\nusing namespace std;\n\nchar getChar()\n{\n char c;\n cin >> c;\n return c;\n}\n\nint main()\n{\n // it doesn\'t work\n const char input1 = getChar();\n const char input2 = getChar();\n\n // it it works\n //const char input1 = \'R\';\n //const char input2 = \'X\';\n\n char result = getChar();\n switch(result)\n {\n case input1:\n cout << "input1" << endl;\n break;\n case input2:\n cout << "input2" << endl;\n break;\n }\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n