无法在 C++ 中返回字符

问题描述

这是我的代码

char pattern(int x){
  if (x < 13){
    return "a";
  }
  if (x < 26){
    return "b";
  }
}

错误invalid conversion from 'const char*' to 'int' 我尝试在网上搜索错误,但所有资源仅适用于 C。请指教。

解决方法

您的问题是双引号表示“C 字符串”。你需要返回的是字符“a”,在C++中是'a',字符“b”是'b'。试试这个:

char pattern(int x){
  if (x < 13){
    return 'a';
  }
  if (x < 26){
    return 'b';
  }
}
,

使用此代码

char pattern(int x){
  if (x < 13){
    return 'a';
  }
  if (x < 26){
    return 'b';
  }
}

使用“a”代替“a”,b 也一样