我有两个班级: 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" ) ;
}发布于 2010-12-06 06:54:09
您已经为Person声明了一个默认构造函数
public:
Person(void);但从未定义过它。
在这种情况下,您不需要拥有自己的默认构造函数-删除声明,让编译器为您生成一个默认构造函数。或者,您可以定义一个不做任何事情:
Person::Person()
{
}发布于 2010-12-06 06:54:12
您在未定义的情况下声明了Person::Person(void);。
如果您删除了这一行,您将会很好。
发布于 2010-12-06 06:53:12
您在第10行声明了Person::Person(),但从未定义过它。
https://stackoverflow.com/questions/4361780
复制相似问题