2016-05-07 12 views
0

と一致することができません。関数定義、私は<strong>ボックス</strong>と呼ばれるクラスが<strong>Entitiyエンティティで</strong></p> <p>、私は<code>getWeight()</code>機能を持つ基本クラスから継承したテンプレート

double Entity::getWeight() { 
    return weight; 
} 

この関数をBoxクラスでオーバーライドしたいと考えています。私はこれをしました。

template <class T> 
double Box<T>::getWeight() { 
    return weight + inWeight; 
} 

しかし、それは私にこのエラー

Error C2244 'Entity::getWeight': unable to match function definition to an existing declaration 

なぜ私はこのエラーを取得していますを与えますか?

EDIT: Entityクラス

class Entity { 
    public: 
     Entity(double weight_in, double length_in, double width_in); 
     Entity(); 

     double getWidth(); 
     void setWidth(double); 
     double getLength(); 
     void setLength(double); 
     double getWeight(); 
     void setWeight(double); 

    protected: 
     double weight; 
     double length; 
     double width; 
}; 

Boxクラス

#include "entity.h" 

template <class T> 
class Box : public Entity{ 
    public: 
     Box(double weight_in, double length_in, double width_in, double maximumAllowedWeight_in); 
     Box(); 
     Box(Box<T>&); 
}; 
+0

「エンティティ」はどのように宣言していますか?関連コードを表示してください。 – songyuanyao

答えて

1

あなたが外でそれを定義する前に、クラス定義内の関数を宣言する必要があります。 (または、クラス内で定義することもできます)

template <typename T> 
class Box : public Entity { 
    double getWeight(); 
}; 

は、定義を有効にします。

constというマークを付けることをおすすめします。

2

アランがEntityクラスについても述べたことを行うべきです。また、BoxのgetWeight()メソッドが呼び出されると予想される場合は、Entityタイプオブジェクトとして宣言されたBox型オブジェクトから呼び出すときに、実際にオーバーライドするようにvirtualキーワードを追加する必要があります(遅延バインド):

class Entity { 
    float weight = 10; 
    virtual double getWeight(){ 
     return weight; 
    } 
}; 

参考:https://en.wikipedia.org/wiki/Virtual_function

関連する問題

 関連する問題