[英]How to fix expression must have a constant value in C++
我正在尝试编写一个散列字符串以在开关中使用它的程序,当我调用 hash function 时出现错误: expression must have a constant value
有问题的代码是:
unsigned int hashString(const std::string string_input, int hash_seed)
{
{
return !string_input[0]
? 33129 : (hashString(string_input, 1)
* 54) ^ string_input[0];
}
}
bool evaluateString(std::string str)
{
string first_word;
stringstream inputStream { str };
commandStream >> first_word;
switch (hashString(first_word, 0))
{
case hashString(ipo::Io::STRING_HELLO, 0):
/* code */
break;
default:
break;
}
}
错误发生在这里: case hashString(ipo::Io::STRING_HELLO, 0):
它标志着ipo
是一个问题
我该如何解决
谢谢您的帮助
你可能想要
constexpr unsigned int hashString(const std::string_view s, int hash_seed = 33129)
{
unsigned int res = hash_seed;
for (auto rit = s.rbegin(); rit != s.rend(); ++rit) {
res = (res * 54) ^ *rit;
}
return res;
}
接着
bool evaluateString(const std::string& str)
{
std::stringstream commandStream{str};
std::string first_word;
commandStream >> first_word;
switch (hashString(first_word))
{
case hashString(ipo::Io::STRING_HELLO): // STRING_HELLO should be a constexpr string_view
/* code */
break;
default:
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.