这是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)
{
}
#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++的类,必须有默认构造函数,带参数的构造函数不是必须的,根据需要添加,如果将默认构造函数添加参数改成带参数的构造函数,编译器由于找不到默认构造函数而报错。