-1

那是我用 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. 为什么它给我这样的错误?

4

2 回答 2

7

代替

~library_card();

~library_card() {}

您还没有实现析构函数,这正是您的链接器所说的。

或者,如果你不知道为什么需要这个析构函数,你也可以删除这个字符串。

于 2012-06-15T10:08:23.007 回答
0

没有函数体~library_card();。只需删除该析构函数或填充它,无论您喜欢什么。

当您在那里时,请阅读有关虚拟析构函数的内容,并考虑您的类是否需要它们。

于 2012-06-15T10:09:54.853 回答