8

私はC++で継承を扱っています。2 つの配列の足し算と引き算のプログラムを書きたかったのです。私のコードは次のとおりです。

#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;

class root
{
    protected :

            int size;
            double *array;

    public :

        virtual ~root() {}
        virtual root* add(const root&) = 0;
        virtual root* sub(const root&) = 0;
        virtual istream& in(istream&, root&) = 0;

        virtual int getSize() const = 0;
        virtual void setSize(int);
        virtual int getAt(int) const = 0;
};

class aa: public root
{

    public :

        aa();
        aa(int);
        aa(const aa&);
        root* add(const root& a);
        root* sub(const root& a);
        istream& in(istream&, root&){}
        int getSize() const;
        void setSize(int);
        int getAt(int) const;
};

class bb: public root
{
public:
    bb() { }
    bb(const bb& b) { }
    root* add(const root& a);
    root* sub(const root& a);
    istream& in(istream&, root&){}
    int getSize() const{}
    void setSize(int){}
    int getAt(int) const{}
};

aa::aa()
{
    size = 0;
    array = NULL;
}

aa::aa(int nsize)
{
    size = nsize;
    array = new double[size+1];
    for(int i=0; i<size; i++)
        array[i] = 0;
}

root* aa::add(const root& a)
{
    for (int i=0; i<a.getSize(); i++)
        array[i] += a.getAt(i);
    return new aa();
}

root* aa::sub(const root& a)
{
}

int aa::getSize() const
{
    return size;
}

void aa::setSize(int nsize)
{
    size = nsize;
    array = new double[size+1];
    for(int i=0; i<size; i++)
        array[i] = 0;
}

int aa::getAt(int index) const
{
    return array[index];
}

root* bb::add(const root& a)
{
    return new bb();
}

root* bb::sub(const root& a)
{

}

int main(int argc, char **argv)
{
}

しかし、奇妙なエラーがあります:

/home/brian/Desktop/Temp/Untitled2.o||In function `root::~root()':|
Untitled2.cpp:(.text._ZN4rootD2Ev[_ZN4rootD5Ev]+0xb)||undefined reference to `vtable for root'|
/home/brian/Desktop/Temp/Untitled2.o||In function `root::root()':|
Untitled2.cpp:(.text._ZN4rootC2Ev[_ZN4rootC5Ev]+0x8)||undefined reference to `vtable for root'|
/home/brian/Desktop/Temp/Untitled2.o:(.rodata._ZTI2bb[typeinfo for bb]+0x8)||undefined reference to `typeinfo for root'|
/home/brian/Desktop/Temp/Untitled2.o:(.rodata._ZTI2aa[typeinfo for aa]+0x8)||undefined reference to `typeinfo for root'|
||=== Build finished: 4 errors, 0 warnings ===|

それらがどこから来たのかわからないので、それらを「修正」する方法を今は知りません。前もって感謝します;)

4

3 に答える 3

16

root::setSizeは純粋仮想として宣言されていないため、定義する必要があります。おそらく、それは他の関数と同じくらい純粋でなければなりません:

virtual void setSize(int) = 0;
                          ^^^

特定のエラーが発生する理由の詳細に興味がある場合: このコンパイラは、クラスの仮想/RTTI メタデータをどこかに生成する必要があり、クラスが非純粋で非インラインの仮想関数を宣言している場合は、それを生成します。その関数の定義と同じ翻訳単位で。定義がないため、生成されず、そのエラーが発生します。

于 2013-05-09T11:31:43.923 に答える
1

あなたroot::setSizeは定義されておらず、純粋仮想関数として宣言されていません。関数の最後に追加= 0する (純粋な仮想にする) か、root::setSize関数を定義します。

于 2013-05-09T11:26:00.290 に答える
1

実装していないからだと思います

    virtual void setSize(int);

root追加するか、純粋な仮想として宣言します=0

于 2013-05-09T11:26:10.023 に答える