gpt4 book ai didi

c++ - 链接错误 C++ 继承

转载 作者:太空宇宙 更新时间:2023-11-04 14:58:02 24 4
gpt4 key购买 nike

我有两个类(class) Person 和 Student。我目前正在尝试从 Person 类派生 Student 类。

但是,我一直收到链接错误。

错误:

[Linker error] undefined reference to `Person::Person()' 

我的代码:

#include <iostream> 
#include <iomanip>
using namespace std;

class Person {

string Name ;

public:
Person(void);

void set(){

cout << "Name:" << endl ;
cin >> Name ;
}

string get_Name(){
return Name ;
}


};

class Student:Person {
int x, Lab1, Lab2, Lab3, Lab4, Lab5, Lab6, LabPoints, Midterm, Final ;
float LabAvg, ExamAvg, Prcnt ;
string StuName ;

public:
Student(void);

void set(int i){
x = i;

cout << "Student " << x << endl << endl ;
cout << "Name:" << endl ;
cin >> StuName ;
cout << "Lab 1 score (1-10): " << endl ;
cin >> Lab1 ;
cout << "Lab 2 score (1-10): " << endl ;
cin >> Lab2 ;
cout << "Lab 3 score (1-10): " << endl ;
cin >> Lab3 ;
cout << "Lab 4 score (1-10): " << endl ;
cin >> Lab4 ;
cout << "Lab 5 score (1-10): " << endl ;
cin >> Lab5 ;
cout << "Lab 6 score (1-10): " << endl ;
cin >> Lab6 ;
cout << "Midterm score (1-100): " << endl ;
cin >> Midterm ;
cout << "Final score (1-100): " << endl ;
cin >> Final ;

LabAvg = (Lab1 + Lab2) / 2.0 ;
LabPoints = (Lab1 + Lab2) ;
ExamAvg = (Midterm + Final) / 2.0 ;
Prcnt = (( ( LabPoints / 60.0 ) * 0.6 ) + ( ( Midterm / 100.0 ) * 0.2) +
( ( Final / 100.0 ) * 0.2)) * 100 ;

}

string get_StuName(){
return StuName ;
}

int get_Lab1(){
return Lab1 ;
}

int get_Lab2(){
return Lab2 ;
}

int get_Lab3(){
return Lab3 ;
}

int get_Lab4(){
return Lab4 ;
}

int get_Lab5(){
return Lab5 ;
}

int get_Lab6(){
return Lab6 ;
}

float get_LabAvg(){
return LabAvg ;
}

int get_LabPoints(){
return LabPoints ;
}

float get_ExamAvg(){
return ExamAvg ;
}

float get_Prcnt(){
return Prcnt ;
}
};

Student::Student(void){
x = 0, Lab1 = 0, Lab2 = 0, Lab3 = 0, Lab4 = 0, Lab5 = 0, Lab6 = 0,
LabPoints = 0, Midterm = 0, Final = 0 ;
LabAvg = 0.0, ExamAvg = 0.0, Prcnt = 0.0 ;
StuName = "" ;
}

int main(){
int MaxNumStu = 10, NumOfRep , i ;
float FPrcnt ;
string LetGrd ;

cout << "Number of Students:" << endl ;
cin >> NumOfRep ;
cout << endl << endl ;

Student obs[MaxNumStu] ;

NumOfRep = ++NumOfRep ;
for(i=1 ; i < NumOfRep ; i++)
obs[i].set(i) ;

cout << endl << "---------------------------------" << endl << endl ;

for(i=1; i < NumOfRep; i++){
cout << obs[i].get_StuName() << endl << endl;
cout << "Lab 1 Score: " << obs[i].get_Lab1() << endl ;
cout << "Lab 2 Score: " << obs[i].get_Lab2() << endl ;
cout << "Lab 3 Score: " << obs[i].get_Lab3() << endl ;
cout << "Lab 4 Score: " << obs[i].get_Lab4() << endl ;
cout << "Lab 5 Score: " << obs[i].get_Lab5() << endl ;
cout << "Lab 6 Score: " << obs[i].get_Lab6() << endl ;
cout << endl << "Average Lab Score: " << setprecision(4) <<
obs[i].get_LabAvg() << endl ;
cout << "Total Lab Points: " << obs[i].get_LabPoints() << endl ;
cout << endl << "Average Exam Score: " << setprecision(4) <<
obs[i].get_ExamAvg() << endl ;

FPrcnt = obs[i].get_Prcnt() ;

if ( FPrcnt >= 90 )
LetGrd = "% A" ;
if ( FPrcnt >= 80 )
if ( FPrcnt < 90 )
LetGrd = "% B" ;
if ( FPrcnt >= 70 )
if ( FPrcnt < 80 )
LetGrd = "% C" ;
if ( FPrcnt >= 60 )
if ( FPrcnt < 70 )
LetGrd = "% D" ;
if ( FPrcnt < 60 )
LetGrd = "% F";

cout << endl << endl << "Overall Grade: " << setprecision(3) << FPrcnt
<< LetGrd << endl;
cout << endl << endl ;
}
system( "pause" ) ;
}

最佳答案

您已经为 Person 声明了默认构造函数:

public: 
Person(void);

但从未定义过它。

在这种情况下,您不需要拥有自己的默认构造函数 - 删除声明并让编译器为您生成默认构造函数。或者,您可以定义一个什么都不做:

Person::Person()
{
}

关于c++ - 链接错误 C++ 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4361780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com