初始化我的char变量时遇到一些问题。
#include "stdafx.h"
#include <iostream>
using namespace std;
class Scout {
public:
Scout(char Name,char Surname) : name(Name), surname(Surname) {
cout << "Constructor 1" << " name = " << name << " surname = " <<surname;
};
private:
char name, surname;
};
int main()
{
Scout Andrew("Duck","Jones");
system("pause");
return 0;
}
错误C2664'Scout :: Scout(Scout&amp;&amp;)':无法将参数1从'const char [5]'转换为'char'
错误(活动)没有构造函数“Scout :: Scout”的实例与参数列表匹配
有没有其他方法来初始化姓名和姓氏?
答案 0 :(得分:2)
构造函数的参数类型为char
,而您在此处传递的参数Andrew("Duck","Jones");
的类型为const char*
。这不起作用。因为这是C ++,所以您可以使用std::string
:
#include "stdafx.h"
#include <iostream>
class Scout {
public:
Scout(std::string Name, std::string Surname) : name(Name), surname(Surname) {
std::cout << "Constructor 1" << " name = " << name << " surname = " <<surname;
};
private:
std::string name, surname;
};
int main()
{
Scout Andrew("Duck","Jones");
system("pause");
return 0;
}