Search code examples
c++compiler-errorsoperators

Compiler error "character constant too long for its type". What's wrong?


I have some code that I'm trying to work on...

#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.";
      }
  }
}

This was adapted from a Bash script I wrote and is one of my first C++ programs. When I compile this, it comes up with these errors...

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’

Could you explain what these mean and how to fix them?

EDIT: I get a new error message now...

.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>

Solution

  • As others have pointed out, you need to use double quotes ("y" instead of 'y') for your strings, otherwise they are character literals.

    In C/C++, there is such a thing as a multi-character literal; its value is a number made up of somehow putting the character codes for the individual characters together in some implementation-defined way. You don't want to ever use them unless you have a really really good reason. They only reason you need to know about them is to understand the warnings and error messages:

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

    ... means that there is no way to compare a string with the number 1919378802, which is what your compiler interprets 'hamburger' to mean.

    Once that is fixed, your new error message:

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

    means that something went wrong with one of the || operators. Maybe one of its operands wasn't actually a boolean expression. The "note" tells you that there is a built-in || for two bools, but that it couldn't be used in this situation.

    Solution: Replace opt = 'Yes' by opt == "Yes".

    The single =, assignment, means that the result of that expression is not a bool but a string, and there is no operator|| for or-ing a boolean with a string.

    Style Note: It's usually considered better style to not use a using namespace std declaration. Instead, explicitly refer to standard library stuff (cout, endl, string, getline) using a std:: prefix, as in std::string.