C++ 错误:开关数量不是整数
我已经在StackOverflow和多谷歌链接上研究了我的问题,但我仍然感到困惑。我想对我来说最好的事情就是问 我正在创建一个简单的命令行计算器。以下是我目前的代码:C++ 错误:开关数量不是整数,c++,string,switch-statement,constant-expression,C++,String,Switch Statement,Constant Expression,我已经在StackOverflow和多谷歌链接上研究了我的问题,但我仍然感到困惑。我想对我来说最好的事情就是问 我正在创建一个简单的命令行计算器。以下是我目前的代码: const std::string Calculator::SIN("sin"); const std::string Calculator::COS("cos"); const std::string Calculator::TAN("tan"); const std::string Calculator::LOG(
const std::string Calculator::SIN("sin");
const std::string Calculator::COS("cos");
const std::string Calculator::TAN("tan");
const std::string Calculator::LOG( "log" );
const std::string Calculator::LOG10( "log10" );
void Calculator::set_command( std::string cmd ) {
for(unsigned i = 0; i < cmd.length(); i++)
{
cmd[i] = tolower(cmd[i]);
}
command = cmd;
}
bool Calculator::is_legal_command() const {
switch(command)
{
case TAN:
case SIN:
case COS:
case LOG:
case LOG10:
return true;
break;
default:
return false;
break;
}
}
强大的互联网,它说字符串被允许在switch语句中使用
感谢大家的帮助。
字符串不能用于C++中的Switter语句。如果/
或者如果
,则需要将其转换为,如下所示:
if (command == "tan")
{
// ...
}
else if (command == "cos")
{
// ...
}
// ...
#include <math.h>
#include <map>
#include <string>
#include <iostream>
class Function
{
public:
// Easy public API that just uses the normal function call symantics
double operator()(double value) { return this->doWork(value);}
virtual ~Function() {}
private:
// Virtual function where the work is done.
virtual double doWork(double value) = 0;
};
// A sin/cos function
class Sin: public Function { virtual double doWork(double value) { return sin(value); } };
class Cos: public Function { virtual double doWork(double value) { return cos(value); } };
// A class that holds all the functions.
// A function name is mapped to a function object.
class FuncMap
{
public:
FuncMap()
{
// Constructor sets up the map
functions["sin"] = &sinFunc;
functions["cos"] = &cosFunc;
}
Function* getFunction(std::string command) const
{
// Default result not found.
Function* result = NULL;
std::map<std::string, Function*>::const_iterator find;
// Look in the map to see if we find the value.
// If it exists then find will not point at end()
if ((find = functions.find(command)) != functions.end())
{
// Get the pointer to the function
result = find->second;
}
return result;
}
private:
Sin sinFunc;
Cos cosFunc;
std::map<std::string, Function*> functions;
};
// Declaring it globally for ease of use.
FuncMap functions;
int main()
{
// SImple example of usage.
Function* func = functions.getFunction("sin");
if (func == NULL)
{
std::cout << "No Function sin()\n";
exit(1);
}
std::cout << "Result: " << (*func)(12.34) << "\n";
}
不确定你读过哪个强大的互联网,但是C++不允许在 Twitter 语句中使用字符串。(不过,C#有。)
C++,你需要将你的<代码>开关>代码>语句转换成一个<代码>的链,如果 ->代码>如果 >代码> EX/<代码>测试相等。您可以使用映射、一系列if,也可以从将命令表示为字符串移动到枚举。从字符串到枚举解析一次,然后像现在一样使用开关。请注意,您的字符串解析可能需要相同的机制(map/if),但根据您的用例,使用一种方法可能会提高可读性。我不会说什么方法更具可读性。在开关中,表达式必须是“一个或一个类类型,对于该类类型,有一个到整型的明确转换”()
字符串类不像char
那样具有“到整型的明确转换”
作为解决方案:
创建一个map
并打开map的值:开关(command\u map[command])
`
执行一组if
/else
而不是switch。更烦人,更难阅读,所以我推荐地图路线
另一方面,对于这样的真正复杂的逻辑,更好的解决方案是改进映射解决方案,以完全摆脱开关
,而是使用函数查找:std::map
。对于您的特定情况,可能不需要它,但对于复杂的很长的查找逻辑,它的速度要快得多。正如其他人和编译器所评论的那样,开关
不允许使用字符串。如果
bool Calculator::is_legal_command() const {
if(command == TAN) return true;
if(command == SIN) return true;
if(command == COS) return true;
if(command == LOG) return true;
if(command == LOG10) return true;
return false;
}
我不认为这会更复杂,而且会尽可能快。你也可以用我的,让它看起来像
bool Calculator::is_legal_command() const {
sswitch(command)
{
scase (TAN):
scase (SIN):
scase (COS):
scase (LOG):
scase (LOG10):
return true;
sdefault():
return false;
}
}
(在返回后出现中断
是死码,因此应避免使用)。而不是开关
我会使用命令模式。然后使用std::map将函数名映射到命令对象
大概是这样的:
if (command == "tan")
{
// ...
}
else if (command == "cos")
{
// ...
}
// ...
#include <math.h>
#include <map>
#include <string>
#include <iostream>
class Function
{
public:
// Easy public API that just uses the normal function call symantics
double operator()(double value) { return this->doWork(value);}
virtual ~Function() {}
private:
// Virtual function where the work is done.
virtual double doWork(double value) = 0;
};
// A sin/cos function
class Sin: public Function { virtual double doWork(double value) { return sin(value); } };
class Cos: public Function { virtual double doWork(double value) { return cos(value); } };
// A class that holds all the functions.
// A function name is mapped to a function object.
class FuncMap
{
public:
FuncMap()
{
// Constructor sets up the map
functions["sin"] = &sinFunc;
functions["cos"] = &cosFunc;
}
Function* getFunction(std::string command) const
{
// Default result not found.
Function* result = NULL;
std::map<std::string, Function*>::const_iterator find;
// Look in the map to see if we find the value.
// If it exists then find will not point at end()
if ((find = functions.find(command)) != functions.end())
{
// Get the pointer to the function
result = find->second;
}
return result;
}
private:
Sin sinFunc;
Cos cosFunc;
std::map<std::string, Function*> functions;
};
// Declaring it globally for ease of use.
FuncMap functions;
int main()
{
// SImple example of usage.
Function* func = functions.getFunction("sin");
if (func == NULL)
{
std::cout << "No Function sin()\n";
exit(1);
}
std::cout << "Result: " << (*func)(12.34) << "\n";
}
#包括
#包括
#包括
#包括
类函数
{
公众:
//简单的公共API,只使用普通函数调用symantics
双运算符()(双值){返回此->doWork(值);}
虚~Function(){}
私人:
//完成工作的虚拟函数。
虚拟双工作(双值)=0;
};
//正反函数
类Sin:public函数{virtualdouble-doWork(double-value){返回Sin(value);};
类Cos:公共函数{virtualdouble-doWork(double-value){返回Cos(value);};
//包含所有函数的类。
//函数名映射到函数对象。
类函数映射
{
公众:
FuncMap()
{
//构造函数设置映射
函数[“sin”]=&sinFunc;
函数[“cos”]=&cosFunc;
}
函数*getFunction(std::string命令)常量
{
//未找到默认结果。
函数*result=NULL;
std::map::const_迭代器find;
//看看地图,看看我们是否找到了价值。
//如果它存在,那么find将不会指向end()
if((find=functions.find(command))!=functions.end()
{
//获取指向函数的指针
结果=查找->秒;
}
返回结果;
}
私人:
辛辛芬;
Cos-cosFunc;
std::map函数;
};
//在全球范围内声明它以便于使用。
FuncMap函数;
int main()
{
//简单的用法示例。
Function*func=functions.getFunction(“sin”);
如果(func==NULL)
{
std::cout编译器错误告诉您需要知道的一切。在switch语句中只能比较整数类型
<>我不确定哪个“强大的互联网”告诉你,但是它是完全错误的。< /p>你不能在C++中切换<代码>字符串<代码>。“强大的互联网,它说在开关语句中可以使用字符串。”在哪里?强大的互联网可能会说“允许在开关语句中使用字符串……在C”中。但是我的错误从来没有在C++中出现过。我的错误主要是我的错。我把它误认为是C……=它可能被C语言混淆了。这使得我的代码不那么漂亮。谢谢亚当。谢谢SOAPBox。我想我必须放弃开关语句。谢谢,我最后做了一些声明,因为它使我的代码变短了。我可以试试地图来了解它。