问题遇到的现象和发生背景
类的初始化
问题相关代码,请勿粘贴截图
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
class Hero
{
int speed;
char* name;
public:
Hero(int speed, char* name) :speed(speed), name(name) {}
};
int main(void)
{
// ----OK
char name[32] = "Athena";
//Hero Athena = { 20, name };
// ----Err
Hero Athena_1 = { 20, "Athena" };
return 0;
}
运行结果及报错内容
E0289 no instance of constructor "Hero::Hero" matches the argument list
我的解答思路和尝试过的方法
参见代码
1如果用一个变量name来初始化,就没问题
2如果直接使用字符串来初始化就报错