【发布时间】:2020-07-20 09:13:03
【问题描述】:
我的任务是设置足球俱乐部的对象并更改其名称。
在课堂上:
Club(string _name, string _colour) :
name(_name), colour(_colour) {}
void newName(char* newName)
{
strcpy(name, newName);
}
主要:
Klub k1("Manchester", "red");
k2.newName("Arsenal");
我收到此错误: 错误 C2664 'std::string Club::newName(char *)': 无法将参数 1 从 'const char [7]' 转换为 'char *'
我包含了 cstring
【问题讨论】:
-
为什么不使用
std::string作为参数?strcpy不适用于std::string类型。 -
一旦你使用
std::string,就没有理由对字符串使用char* -
字符串 newName(char* Name) { name= Name; }
-
void newName(std::string n) { name = n;} -
您传递给函数的字符串文字
"Arsenal"是一个指向 const char 的指针。这就是编译器抱怨的原因。