这是我用C ++(OOP)编写的程序:
#include <iostream>
#include <string>
using namespace std;
class book {
string name,gvari;
double cost;
int year;
public:
book(){};
book(string a, string b, double c, int d) { a=name;b=gvari;c=cost;d=year; }
~book() {}
double setprice(double a) { return a=cost; }
friend ostream& operator <<(ostream& , book&);
void printbook(){
cout<<"wignis saxeli "<<name<<endl;
cout<<"wignis avtori "<<gvari<<endl;
cout<<"girebuleba "<<cost<<endl;
cout<<"weli "<<year<<endl;
}
};
ostream& operator <<(ostream& out, book& a){
out<<"wignis saxeli "<<a.name<<endl;
out<<"wignis avtori "<<a.gvari<<endl;
out<<"girebuleba "<<a.cost<<endl;
out<<"weli "<<a.year<<endl;
return out;
}
class library_card : public book {
string nomeri;
int raod;
public:
library_card(){};
library_card( string a, int b){a=nomeri;b=raod;}
~library_card();
void printcard(){
cout<<"katalogis nomeri "<<nomeri<<endl;
cout<<"gacemis raodenoba "<<raod<<endl;
}
friend ostream& operator <<(ostream& , library_card&);
};
ostream& operator <<(ostream& out, library_card& b) {
out<<"katalogis nomeri "<<b.nomeri<<endl;
out<<"gacemis raodenoba "<<b.raod<<endl;
return out;
}
int main() {
book A("robizon kruno","giorgi",15,1992);
library_card B("910CPP",123);
cout<<A;
cout<<B;
A.setprice(15);
cout<<B;
system("pause");
return 0;
}
//P.S.
//B.printcard();
//A.printbook();
// As I've overloaded the "<<" operators, I've removed these two lines and put "cout<<A" "cout<<B". I hope, I'm right. By the way, error isn't related to these ones.
这是错误,它可能对您有所帮助:
1>Neyw.obj : error LNK2019: unresolved external symbol "public: __thiscall library_card::~library_card(void)" (??1library_card@@QAE@XZ) referenced in function _main
1>c:\users\geo\documents\visual studio 2010\Projects\Newww\Debug\Newww.exe : fatal error LNK1120: 1 unresolved externals
1>Build FAILED.
问题是我无法弄清楚代码有什么问题。有两个类,主要类book
和后继类library_card
。为什么它会给我这样的错误?
答案 0 :(得分:7)
替换
~library_card();
与
~library_card() {}
您还没有实现析构函数,这正是您的链接器所说的。
如果你不知道为什么需要这个析构函数,你也可以删除这个字符串。
答案 1 :(得分:0)
~library_card();
没有功能正文。只需删除该析构函数,或填充它,无论您喜欢哪种。
虽然你在那里,但请阅读virtual destructors,并考虑你的课程是否需要它们。