Mình viết một file Sinhvien.h có nội dung như sau:
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
string id,course,bir,hometown;
float pM,pB,pC,pW;
public:
void setPoint(float pointMath,float pointBiology,float pointC,float pointWeb);
void setInformation(string ID, string classname, string birthday, string hometown);
float getPM()
{
return this->pM;
}
float getpB()
{
return this->pB;
}
float getpC()
{
return this->pC;
}
float getpW()
{
return this->pW;
}
void Input();
float Average(float pM,float pB, float pC, float pW);
void Classification();
void Output();
float Check(float point);
Student(void);
~Student(void);
};
và 1 file Sinhvien.cpp có nội dung:
#include "Sinhvien.h"
using namespace std;
void Student::setPoint(float pointMath,float pointBiology,float pointC,float pointWeb)
{
this->pM=pointMath;
this->pB=pointBiology;
this->pC=pointC;
this->pW=pointWeb;
}
void Student::setInformation(string ID, string classname, string birthday, string hometown)
{
this->id=ID;
this->course=classname;
this->bir=birthday;
this->hometown=hometown;
}
float Student::Check(float point)
{
char ch, s1='Y',s2='y';
while(point <0||point>10)
{
cout<<endl<<"Nhap sai.";
cout<<endl<<"Ban co muon nhap lai";
cin>>ch;
if(ch==s1||ch==s2)
{
cout<<endl<<"Nhap diem: ";
cin>>point;
}
else
{
point=0;
break;
}
}
return point;
}
void Student::Input()
{
string s1,s2,s3,s4;
float f1,f2,f3,f4;
cout<<"Nhap ma sinh vien: ";
cin>>s1;
cout<<endl<<"Nhap lop: ";
cin>>s2;
cout<<endl<<"Nhap ngay sinh: ";
cin>>s3;
cout<<endl<<"Nhap que quan: ";
cin>>s4;
setInformation(s1,s2,s3,s4);
cout<<endl<<"Nhap diem mon toan: ";
cin>>f1;
Check(f1);
cout<<endl<<"Nhap diem mon sinh: ";
cin>>f2;
Check(f2);
cout<<endl<<"Nhap diem mon C: ";
cin>>f3;
Check(f3);
cout<<endl<<"Nhap diem mon web: ";
cin>>f4;
Check(f4);
setPoint(f1,f2,f3,f4);
}
float Student:: Average(float pM,float pB, float pC, float pW)
{
return (pM+pB+pC+pW)/4;
}
void Student::Classification()
{
float arg=Average(pM,pB,pC,pW);
if(arg<5)
cout<<endl<<"Xep loai yeu";
else
if(arg<6)
cout<<endl<<"Xep loai trung binh";
else
if(arg<7)
cout<<endl<<"Xep loai trung binh kha";
else
if(arg<8)
cout<<endl<<"Xep loai kha";
else
if(arg<9)
cout<<endl<<"Xep loai gioi";
else
cout<<endl<<"Xep loai xuat sac";
}
void Student::Output()
{
cout<<endl<<"Diem trung binh: "<<Average(pM,pB,pC,pW);
Classification();
}
void Student::Student()
{
}
void Student::~Student(void)
{
}
file main.cpp:
#include<iostream>
#include "Sinhvien.h"
using namespace std;
int main()
{
Student s;
s.Input();
s.Output();
}
nhưng không hiểu sao khi chạy thì nó lại thông báo lỗi:
main.cpp:(.text+0x1b): undefined reference to `Student::Student()'
main.cpp:(.text+0x27): undefined reference to `Student::Input()'
main.cpp:(.text+0x33): undefined reference to `Student::Output()'
main.cpp:(.text+0x3f): undefined reference to `Student::~Student()'
main.cpp:(.text+0x55): undefined reference to `Student::~Student()'
[Error] ld returned 1 exit status