2013-05-09 19 views
5

は、私はC++での継承を扱っています。私は2つの配列の加算と減算のためのプログラムを書こうと思っていました。 HERESに私のコード:クラスのvtableの `へと未定義の参照`クラスのtypeinfoのへの未定義参照

#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 ===| 

は、彼らがどこから来たのかを「修正」彼ら、今いけないから知りません。ありがとうございます;)

+1

裸のポインタは使用しないでください。ただそこに行かないでください。これはひどく壊れたコードです。 (また、コンストラクタのイニシャライザのリストがどのように機能するかを調べてください。) –

答えて

12

root::setSizeを追加することによって、仮想それは純粋な宣言していないからだと信じているが、それが定義されなければならないことを意味する、純粋仮想と宣言されていません。おそらく、それは他の関数のように純粋である必要があります。

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

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

+1

+1は細部の詳細を与えるために! – Nick

1

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

1

私はあなたがroot

virtual void setSize(int); 

を実装したり=0

関連する問題