表达式必须具有整型或无作用域枚举类型

Expression must have integral or unscoped enum type

本文关键字:作用域 枚举 类型 整型 表达式      更新时间:2023-10-16

可以将两个char const*参数相加吗?代码应该看起来像这样。我需要cab一样char const。如果有人知道如何制作这个,请帮助我:)提前致谢

char const *a = "something";
char const *b = " more";
char const *c = a + b;

如果您希望字符串像其他语言中的对象一样工作,请使用std::string

std::string a = "something";
std::string b = " more";
std::string c = a + b;

如果需要将生成的字符串传递给需要const char *的东西,则可以对字符串调用 c_str() 函数。

你应该有一个char数组来创建连接的结果:

char* result; 
result = static_cast<char*>( calloc ( strlen( a) + strlen( b) + 1, 
                                                             sizeof( char)));
strcpy( result, a); // copy string a into the result
strcat( result, b); // append b to the result

但是,在C++中,您应该使用std::string

std::string a = "something";
std::string b = " more";
std::string result = a + b;