c++ 入门求指导错误error C2512: “Person”: 没有合适的默认构造函数可用

这是Person.h

#pragma once
class Person
{
public:
Person(char*,int);
int Person::getHeight(Person);
char* Person::getName(Person);
private:
char* name;
int height;
};
这是Person.c
#include "stdafx.h"
#include "Person.h"

Person::Person(char* name,int height)
{
Person::name=name;
Person::height=height;
}
这是Student.h Student继承了Person
#pragma once
#include "person.h"
class Student :
public Person
{
public:
Student(void);
Student(char *,int ,int );
~Student(void);

private:
char *name;
int grade;
int height;
};

这是Student.cpp

#include "StdAfx.h"
#include "Student.h"

Student::Student(char *name,int grade,int height){ //就是这个地方出现了这个错误,求解答啊
Student::grade=grade;
Student::height=height;
Student::name=name;
}

Student::~Student(void)
{
}

Person.h:

#pragma once
class Person
{
public:
Person();                                 //添加了一个默认构造函数
Person(char*,int);                        //这里缺少默认构造函数
int Person::getHeight(Person);
char* Person::getName(Person);
private:
char* name;
int height;
};
这是Person.c
#include "stdafx.h"
#include "Person.h"

Person::Person()
{    //请自行添加初始化代码
}

Person::Person(char* name,int height)
{
Person::name=name;
Person::height=height;
}
这是Student.h   Student继承了Person
#pragma once
#include "person.h"
class Student :
public Person
{
public:
Student(void);
Student(char *,int ,int );
~Student(void);

private:
char *name;
int grade;
int height;
};

这是Student.cpp

#include "StdAfx.h"
#include "Student.h"

Student::Student(char *name,int grade,int height){  //就是这个地方出现了这个错误,求解答啊
//Student的基类Person缺少默认构造函数
Student::grade=grade;
Student::height=height;
Student::name=name;
}

Student::~Student(void)
{
}

如果满意,欢迎采纳我的解答,谢谢
如有疑问,欢迎追问

追问

为什么需要这样的构造函数啊,有什么意义啊?我刚从JAVA转过来各种不适应

追答

C++比Java严格,但也比Java自由度大。
C++的类,必须有默认构造函数,带参数的构造函数不是必须的,根据需要添加,如果将默认构造函数添加参数改成带参数的构造函数,编译器由于找不到默认构造函数而报错。

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网