【问题标题】:Compiler error "character constant too long for its type". What's wrong?编译器错误“字符常量对于它的类型来说太长了”。怎么了?
【发布时间】:2012-02-26 03:46:19
【问题描述】:

我正在尝试处理一些代码...

#include <iostream>
#include <string>

int main()
{
  std::cout << "Hello. Welcome to Delicious Drive Up. What would you like to order?\n";
  std::cout << "\nOur menu is-";
  std::cout << "...";
  std::cout << "\nOrder here > ";
  std::string choice;
  std::getline(cin, choice);
  if (choice == 'hamburger' || choice == 'Hamburger')
  {
      std::cout << "We don't have any ham. Is a Chickenburger all right? y/n. > ";
      std::string opt;
      std::getline(cin, opt);
      if (opt == 'y' || opt == 'Y' || opt == 'yes' || opt = 'Yes')
      {
          std::cout << "Here's your chickenburger.";
      }
  }
}

这是改编自我编写的 Bash 脚本,是我的第一个 C++ 程序之一。当我编译它时,它会出现这些错误......

test.cpp:19:15: warning: character constant too long for its type
test.cpp:19:40: warning: character constant too long for its type
test.cpp:23:44: warning: multi-character character constant
test.cpp:23:59: warning: multi-character character constant
test.cpp: In function ‘int main()’:
test.cpp:19: error: no match for ‘operator==’ in ‘choice == 1919378802’
test.cpp:19: error: no match for ‘operator==’ in ‘choice == 1919378802’
test.cpp:23: error: no match for ‘operator==’ in ‘opt == 'y'’
test.cpp:23: error: no match for ‘operator==’ in ‘opt == 'Y'’
test.cpp:23: error: no match for ‘operator==’ in ‘opt == 7955827’

您能解释一下这些是什么意思以及如何解决它们吗?

编辑:我现在收到一条新的错误消息...

.test.cpp: In function ‘int main()’:
.test.cpp:23: error: no match for ‘operator||’ in ‘((std::operator== [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& opt))), ((const char*)"y")) || std::operator== [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& opt))), ((const char*)"Y"))) || std::operator== [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& opt))), ((const char*)"yes"))) || opt’
.test.cpp:23: note: candidates are: operator||(bool, bool) <built-in>

【问题讨论】:

    标签: c++ compiler-errors operators


    【解决方案1】:

    您正在使用单引号将您的字符串括起来。

    if (choice == 'hamburger' || choice == 'Hamburger')
    

    把它改成双引号。

    if (choice == "hamburger" || choice == "Hamburger")
    

    单引号用于定义单个字符,双引号用于定义字符串字面量。

    【讨论】:

      【解决方案2】:

      您使用单引号将字符串括起来。你需要改变

      if (choice == 'hamburger' || choice == 'Hamburger')
      

      if (choice == "hamburger" || choice == "Hamburger")
      

      当然,'Yes''yes' 也是如此。

      至于第二个问题,您尝试将单个字符与字符串进行比较。您还需要将您的 'Y' 视为字符串:

      if (opt == "y" || opt == "Y" || opt == "yes" || opt == "Yes")
             //  ^^^ Note the double quotes also on single characters
      

      【讨论】:

        【解决方案3】:

        正如其他人指出的那样,您的字符串需要使用双引号("y" 而不是'y'),否则它们是字符文字。

        在 C/C++ 中,存在多字符文字这样的东西;它的值是一个数字,它以某种实现定义的方式将各个字符的字符代码组合在一起。除非你有一个非常好的理由,否则你永远不想使用它们。您需要了解它们的唯一原因是了解警告和错误消息:

        test.cpp:19: error: no match for ‘operator==’ in ‘choice == 1919378802’
        

        ... 表示无法将字符串与数字 1919378802 进行比较,这是您的编译器将 'hamburger' 解释为的意思。

        一旦修复,您的新错误消息:

        .test.cpp:23: error: no match for ‘operator||’ in ...
        .test.cpp:23: note: candidates are: operator||(bool, bool) <built-in>
        

        表示|| 运算符之一出现问题。也许它的一个操作数实际上并不是一个布尔表达式。 “注释”告诉你有一个内置的|| 用于两个bools,但是在这种情况下它不能使用。

        解决方案:将opt = 'Yes' 替换为opt == "Yes"

        单个=,赋值,意味着该表达式的结果不是布尔值而是字符串,并且没有operator|| 用于与字符串进行布尔运算。

        样式注意:通常认为不使用using namespace std 声明的样式更好。相反,使用std:: 前缀明确引用标准库内容(coutendlstringgetline),如std::string

        【讨论】:

          猜你喜欢
          • 2015-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多