V
vsk
In my AP Comp. class, we wrote a Symbolic Algebra program in Java that
is completely based on one interface: IExpression.
I want to port my Java code to C++, for experience, and I'm having a
few issues.
C++ doesn't (to my knowledge) have an equivalent of an Interface, so
I;
class IExpression {
public:
IExpression() {};
virtual bool hasVar() ;
virtual double eval(double);
virtual string getStr();
virtual string getSmart();
virtual bool equals(IExpression&);
virtual IExpression simplify();
virtual IExpression derivative();
};
Once the "interface" or base-class was done, I wanted to implement it
with a simple class from my project: Number;
class Number : public virtual IExpression {
private:
double value;
void init();
public:
Number(double);
bool equals(Number &that);
bool hasVar();
double eval(double);
string getStr();
string getSmart();
bool equals(IExpression&);
IExpression simplify();
IExpression derivative();
};
I wrote the implementation of Number's methods in the header, and I
wont bother posting (most of) them.
The one that's giving me hell is;
bool Number::equals(IExpression &that) {
if (typeid(this) == typeid(that)) {
return this->equals(reinterpret_cast<Number&> (that));
} else {
return false;
}
}
bool Number::equals(Number &that) {
return this->value == that.value;
}
C++ has given me arcane error messages, and I don't know what I'm
doing that's so horribly incorrect.
I think it's a down-casting problem in equals(), but it's also telling
me that I have an "undefined reference to vtable".
How can I fix this?
is completely based on one interface: IExpression.
I want to port my Java code to C++, for experience, and I'm having a
few issues.
C++ doesn't (to my knowledge) have an equivalent of an Interface, so
I;
class IExpression {
public:
IExpression() {};
virtual bool hasVar() ;
virtual double eval(double);
virtual string getStr();
virtual string getSmart();
virtual bool equals(IExpression&);
virtual IExpression simplify();
virtual IExpression derivative();
};
Once the "interface" or base-class was done, I wanted to implement it
with a simple class from my project: Number;
class Number : public virtual IExpression {
private:
double value;
void init();
public:
Number(double);
bool equals(Number &that);
bool hasVar();
double eval(double);
string getStr();
string getSmart();
bool equals(IExpression&);
IExpression simplify();
IExpression derivative();
};
I wrote the implementation of Number's methods in the header, and I
wont bother posting (most of) them.
The one that's giving me hell is;
bool Number::equals(IExpression &that) {
if (typeid(this) == typeid(that)) {
return this->equals(reinterpret_cast<Number&> (that));
} else {
return false;
}
}
bool Number::equals(Number &that) {
return this->value == that.value;
}
C++ has given me arcane error messages, and I don't know what I'm
doing that's so horribly incorrect.
I think it's a down-casting problem in equals(), but it's also telling
me that I have an "undefined reference to vtable".
How can I fix this?