I try to compile the following code :
#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(){
}
Here are the needed classes:
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);
Pile.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;
}
I get 2 errors when i try to compile this code:
1) error: no match for call to ‘(Element) (Nombre, Nombre)’
Fraction f = (*tab[i])(pile.pop(),pile.pop());
Here, *tab[i] is a pointer on Element but the instance are supposed to be Plus or Moins ( which are both derived from Operateur, and Operateur derived from Element). I understand the problem: i only implemented operator() in Plus and Moins classes so the compiler can't find it in Element, but how can i fix this ?
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’
I use Pile and i try to push() with an Element object. As Nombre is derivated from Element, shouldn't i be able to use an Element object instead of a Nombre object ?
I have been looking for answers for hours, but i still don't understand. I feel like i have not understood something very basic.
You need to have a virtual Fraction operator()(Fraction f1, Fraction f2);
in Element.
But that won't work so easily, because Fraction is derived from Element, so has not been declared yet. You could try virtual Element operator()(Element e1, Element e2);
. But you will find that returning a non-homogenous type from Plus and Moins will be another issue.
You are pushing non-homogenous types onto the Pile. That won't work. Even though Fraction is derived from Nombre, it isn't exactly a Nombre, and will slice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.