我明白了
error: could not convert 'p1' from 'Person (*)()' to 'Person'
Run Code Online (Sandbox Code Playgroud)
每当我使用默认构造函数时(当我创建Person p1时),我知道这是因为我使用的是char数组,但我必须使用它,我不能使用字符串
我也收到了2个警告
warning: converting to non-pointer type 'char' from NULL [-Wconversion-null]|
Run Code Online (Sandbox Code Playgroud)
在默认构造函数中
warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
Run Code Online (Sandbox Code Playgroud)
当我创建Person p2时
所以这是我的代码
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
class Person{
private:
char* name;
char gender;
int age;
public:
Person();
Person(char*, char, int);
friend void printInfo(Person);
};
Person::Person()
:name(NULL), gender(NULL), age(0) // this results in the first warning
{
}
Person::Person(char* n, char g, int a)
:name(n), gender(g), age(a)
{
}
void printInfo(Person p){
cout << "Name: " << p.name << endl;
cout << "Age: " << p.age << endl;
cout << "Gender: " << p.gender << endl;
}
int main()
{
Person p1(); // this results in an error
printInfo(p1);
Person p2("Max", 'm', 18); // this results in the second warning
printInfo(p2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)