簡體   English   中英

如何修復表達式在 C++ 中必須有一個常量值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM