switch 语句中从 'const char*' 到 'char' 的转换无效
Invalid conversion from 'const char*' to 'char' in switch statement
我正在尝试制作一个切换语句,该语句采用char数组的第一个字符并大写。我仍然是一个初学者,所以我不知道我在做什么错!我遇到的错误是:
error: invalid conversion from 'const char*' to 'char' [-fpermissive]
return "A";
这是我的代码。
#include <iostream>
using namespace std;
char capitalize(char x)
{
switch(x)
{
case a:
return "A";
break;
default:
break;
}
}
int main()
{
char name[50];
cout << "What is your name?" << endl;
cin.getline(name, 49);
cout << "Hello there, " << name << "." << endl;
return 0;
}
我很抱歉,如果这似乎是一个愚蠢的问题。
用 return 'A';
return "A";
-
'A'
的类型 -char
,它是一个ASCII字符 -
"B"
的类型 -const char *
,这是一个无效的ascii字符串 -
L'C'
的类型 -wchar_t
,它是一个单一字符 -
L"D"
的类型 -const wchar_t*
,这是一个无效的Unicode字符串
相关文章:
- 将无符号char*转换为std::istream*C++
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 将函数参数"const char*"转换为"std::string_view"是
- C ++如何在使用"tolower"时将char转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- JavaCPP 错误:无法将参数"1"的"char*"转换为"Abc*
- 为什么从 char 转换为 std::byte 可能是未定义的行为?
- 如何在虚幻引擎4中将char*转换为TCHAR?
- C++:将模板类型 char 转换为 std::string
- strcmp/char* 转换无法按预期工作
- C++中的SQLite char*转换
- 无法从 const char* 转换为 const char *&
- 在 C++ 中将 char 转换为 int,而无需符号位传播
- 如果必须包含西里尔字符,如何将 char* 转换为 std::string?
- 试图使用strCMP时的char转换错误
- 无法从'const char *'转换为'LPCTSTR'
- 将 std::vector<char> 转换为字符* 会导致字符缺陷
- 使用 static_cast 将 char 转换为 int c++
- 如何在 C++(MFC) 中将 char* 转换为 LPCTSTR
- 无法将const char转换为字符串构造字符串