2017-11-19 6 views
-2

C++でのプログラミングを始めたばかりで、「変数型 'Rectは抽象クラスです」という理由が分かりません。 私のコードの一部はここにあります: C++のクラスからオブジェクトを作成する

main(){ 
    Rect r1(10,5,"R1"); 
return 0; 
} 
class Rect : public Figure 
{ 
public: 
    Rect(int l, int h,std::string Label) : Figure(l), _h(h),_Label(Label) {}; 
    ~Rect(){}; 
    std:: vector<std::string> toString() const; 

protected: 
    int _h; 
    int _l; 
    std::string _Label; 
}; 

class Figure 
{ 
public: 
    Figure(int l):_l(l){} 
    virtual std::vector<std::string> toStrings() const =0; 
    virtual ~Figure(); 
protected: 
    int _l; 
}; 
+0

これは標準エラーです。つまり、オブジェクトを作成しているクラスに対して、純粋な仮想メソッドが実装されていないことを意味します。この場合、Rectは 'std :: vector toStrings()const'を実装する必要があります。 –

+2

'toString()' vs 'toStrings()' –

答えて

1

これは標準誤差である、あなたの助けのために事前にありがとうございます。 これは、オブジェクトを作成しているクラスに対して、いくつかの純粋仮想メソッドが実装されていないことを意味します。

この場合、Rectはstd::vector<std::string> toStrings() constを実装する必要があります。それを修正するには、次の

class Rect : public Figure 
{ 
public: 
    Rect(int l, int h,std::string Label) : Figure(l), _h(h),_Label(Label) {}; 
    ~Rect(){}; 
    std:: vector<std::string> toStrings() const override 
    { 
    return {}; 
    } 

protected: 
    int _h; 
    int _l; 
    std::string _Label; 
}; 

ようなエラーを発見するのに役立ちますoverrideキーワードがあるC++ 11ので。

関連する問題