c++ - 我不断收到 'no match for call to' 错误

标签 c++

#include <iostream>
#include <string>

using namespace std;


// Turns a digit between 1 and 9 into its english name
// Turn a number into its english name

string int_name(int n)
{

  string digit_name;
  {
    if (n == 1) return "one";
     else if (n == 2) return "two";
     else if (n == 3) return "three";
     else if (n == 4) return "four";
     else if (n == 5) return "five";
     else if (n == 6) return "six";
     else if (n == 7) return "seven";
     else if (n == 8) return "eight";
     else if (n == 9) return "nine";

    return "";
  }


  string teen_name;
  {
    if (n == 10) return "ten";
     else if (n == 11) return "eleven";
     else if (n == 12) return "twelve";
     else if (n == 13) return "thirteen";
     else if (n == 14) return "fourteen";
     else if (n == 14) return "fourteen";
     else if (n == 15) return "fifteen";
     else if (n == 16) return "sixteen";
     else if (n == 17) return "seventeen";
     else if (n == 18) return "eighteen";
     else if (n == 19) return "nineteen";

    return "";
  }


  string tens_name;
  {
    if (n == 2) return "twenty";
     else if (n == 3) return "thirty";
     else if (n == 4) return "forty";
     else if (n == 5) return "fifty";
     else if (n == 6) return "sixty";
     else if (n == 7) return "seventy";
     else if (n == 8) return "eighty";
     else if (n == 9) return "ninety";

    return "";
  }

  int c = n; // the part that still needs to be converted
  string r; // the return value

  if (c >= 1000)
  {
     r = int_name(c / 1000) + " thousand";
     c = c % 1000;
  }

  if (c >= 100)
  {
     r = r + " " + digit_name(c / 100) + " hundred";
     c = c % 100;
  }

  if (c >= 20)
  {
     r = r + " " + tens_name(c /10);
     c = c % 10;
  }

  if (c >= 10)
  {
     r = r + " " + teen_name(c);
     c = 0;
  }

  if (c > 0)
     r = r + " " + digit_name(c);

  return r;
}

int main()
{
  int n;
  cout << endl << endl;
  cout << "Please enter a positive integer: ";
  cin >> n;
  cout << endl;
  cout << int_name(n);
  cout << endl << endl;

  return 0;
}

我不断收到此错误代码:

intname2.cpp: In function âstd::string int_name(int)â:
intname2.cpp:74: error: no match for call to â(std::string) (int)â
intname2.cpp:80: error: no match for call to â(std::string) (int)â
intname2.cpp:86: error: no match for call to â(std::string) (int&)â
intname2.cpp:91: error: no match for call to â(std::string) (int&)â

最佳答案

digit_nameteen_name 等被定义为变量时,您将它们用作函数。如果你想像那样使用它们,你需要在你的 int_name 函数之前定义它们,如下所示:

string digit_name(int n)
{
 if (n == 1) return "one";
 else if (n == 2) return "two";
 else if (n == 3) return "three";
 else if (n == 4) return "four";
 else if (n == 5) return "five";
 else if (n == 6) return "six";
 else if (n == 7) return "seven";
 else if (n == 8) return "eight";
 else if (n == 9) return "nine";

 return "";
}

关于c++ - 我不断收到 'no match for call to' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2844775/

相关文章:

c++ - 简单的 std::regex_search() 代码无法使用 Apple clang++ -std=c++14 编译

c++ - Lua - 反射 - 获取对象上的函数/字段列表?

c++ - 无法归档所有数据

c++ - 在 C++ 中处理指向层次结构中成员函数的指针

c++ - 递归下降解析器,用自身初始化变量,困境

c++ - 在模板虚类层次结构中调用基类方法

c++ - 如何设置一个以字符串为键、以 ostream 为值的映射?

c++ - 哪种数据结构更适合用于查找句子是否包含唯一字符?

c++操纵器,是否可以将setprecision manip与setfill manip结合使用?

c++ - 是否可以使用带有 bison/yacc 的逆波兰符号为一种语言生成解析器?