2017-11-18 4 views
0

のための一致なし:C++テンプレートや、派生クラス、私は次のコードをコンパイルしようとする機能

Fraction.hpp:

#include <iostream> 
#include "Element.cpp" 
using namespace std; 


class Fraction : public Nombre{ 

private: 
    int nume; 
    int deno; 
    static int pgcd(int x, int y); 

public: 
    Fraction(int n, int d); 
    Fraction(int n); 
    int getNume() const; 
    int getDeno() const; 
    virtual string getType(); 
    Fraction operator+(const Fraction &) const; 
    Fraction operator-(const Fraction &) const; 

}; 

ostream &operator<<(ostream &os,const Fraction &f); 
Fraction operator+(const int &n,const Fraction &f); 

パイルここ

#include "Fraction.cpp" 
#include "Pile.cpp" 
#include "Plus.cpp" 
#include <iostream> 

using namespace std; 

Nombre calcul(Element* tab [], int nbElement){ 

    Pile<Nombre> pile = Pile<Nombre>(30); 
    for(int i = 0 ; i < nbElement ; i++){ 
     if(tab[i]->getType() == "o"){ 
     Fraction f = (*tab[i])(pile.pop(),pile.pop()); 
     pile.push(f); 
     } 
     else if(tab[i]->getType() == "n"){ 
     pile.push(*(tab[i])); 
     } 
    } 
    return pile.pop(); 

} 

int main(){ 

} 

が必要なクラスであります.hpp:

template <typename T> class Pile{ 

    private: 
    int size; 
    int top; 
    T* stacktab; 

    public: 
    Pile<T>(); 
    Pile<T>(int s); 
    bool isEmpty(); 
    bool isFull(); 
    bool push(T elt); 
    T pop(); 
    void afficher(ostream &flux); 

}; 

Element.cpp:

#include <string> 
using namespace std; 

class Element{ 

public: 
    virtual string getType() = 0; 
}; 


class Operateur : public Element{ 
public: 
    virtual string getType() ; 
}; 

class Nombre : public Element{ 
public: 
    virtual string getType() ; 
}; 

string Operateur::getType() { 
    return "o"; 
} 

string Nombre::getType() { 
    return "n"; 
} 

Plus.cpp:

class Plus : public Operateur{ 
public: 
    Fraction operator()(Fraction f1, Fraction f2); 
}; 

class Moins : public Operateur{ 
public: 
    Fraction operator()(Fraction f1, Fraction f2); 
}; 

Fraction Plus::operator()(Fraction f1,Fraction f2){ 
    return f1 + f2; 
} 

Fraction Moins::operator()(Fraction f1, Fraction f2){ 
    return f1 - f2; 
} 

私はこのコードをコンパイルしようとすると、2つのエラーを取得:

ここ
1) error: no match for call to ‘(Element) (Nombre, Nombre)’ 
     Fraction f = (*tab[i])(pile.pop(),pile.pop()); 

、*タブ[i]は要素上のポインタであるが、インスタンスはPlusまたはMoins(Operateurから派生したものとElementから派生したOperateurの両方)であるはずです。私は問題を理解しています:私は演算子()PlusとMoinsクラスで実装されているので、コンパイラはElementでそれを見つけることができませんが、これをどのように修正できますか?

2) error: no matching function for call to ‘Pile<Nombre>::push(Element&)’ 
     pile.push(*(tab[i])); 
note: candidate: bool Pile<T>::push(T) [with T = Nombre] 
template <typename T> bool Pile<T>::push(T elt){ 
          ^
note: no known conversion for argument 1 from ‘Element’ to ‘Nombre’ 

私はPileを使用しており、Elementオブジェクトでpush()しようとしています。 NombreはElementから派生しているので、Nombreオブジェクトの代わりにElementオブジェクトを使用することはできませんか?

私は何時間も答えを探していましたが、まだ分かりません。私は非常に基本的な何かを理解していないように感じる。

答えて

0

エレメントにはvirtual Fraction operator()(Fraction f1, Fraction f2);が必要です。

しかし、FractionはElementから派生しているため、まだ簡単には宣言されていないため、簡単には機能しません。あなたはvirtual Element operator()(Element e1, Element e2);を試すことができます。しかし、PlusとMoinsから非同種の型を返すことも別の問題であることがわかります。

パイルに非同質タイプを押し込んでいます。それは動作しません。 FractionはNombreから派生していますが、Nombreではなく、スライスします。

関連する問題