I know this has been posted plenty of times before, but none of the solutions were able to help. I'm trying to create multiple subclasses from one superclass (in this instance, Value->Fraction->RationalFraction and Value->Fraction->IrrationalFraction), but I keep getting this error. I assume it's from a circular inclusion, but I don't know how to make the Fraction code work without it. When I construct Fraction, depending on the constructor, it creates an Irrational or Rational Fraction, but then I need to include Fraction in those subclasses, and it kind of creates a cluster. The error is coming in RationalFraction and IrrationalFraction, and I can't seem to get around it. Is there a smoother way to implement this or at the least a way to fix the error? Sorry if this has been answered already, I'm still new to polymorphism.
Value.h
#ifndef VALUE_H
#define VALUE_H
#include <string>
using namespace std;
class Value
{
public:
Value();
virtual ~Value();
string type;
virtual string getType() = 0;
protected:
private:
virtual Value* simplify() = 0;
};
#endif // VALUE_H
Fraction.h
#ifndef FRACTION_H
#define FRACTION_H
#include "Value.h"
#include "RationalFraction.h"
#include "IrrationalFraction.h"
#include <string>
using namespace std;
class Fraction: public Value
{
private:
RationalFraction* rtF;
virtual Fraction* simplify() = 0;
IrrationalFraction* irF;
public:
Fraction(int n, int d);
Fraction(string n, int d);
virtual ~Fraction();
virtual string getType() = 0;
int numer;
int denom;
protected:
};
#endif // FRACTION_H
Fraction.cpp
#include "Fraction.h"
#include <iostream>
using namespace std;
Fraction::Fraction(int n, int d) {
rtF = new RationalFraction(n,d);
}
Fraction::Fraction(string n, int d){
irF = new IrrationalFraction(n, d);
}
Fraction::~Fraction()
{
delete rtF;
delete irF;
}
IrrationalFraction.h
#ifndef IRRATIONALFRACTION_H
#define IRRATIONALFRACTION_H
class IrrationalFraction : public Fraction
{
public:
IrrationalFraction(string n, int d);
virtual ~IrrationalFraction();
protected:
private:
IrrationalFraction* simplify();
};
#endif // IRRATIONALFRACTION_H
RationalFraction.h
#ifndef RATIONALFRACTION_H
#define RATIONALFRACTION_H
using namespace std;
class RationalFraction: public Fraction
{
public:
RationalFraction(int n, int d);
virtual ~RationalFraction();
int numer;
int denom;
protected:
private:
RationalFraction* simplify();
};
#endif // RATIONALFRACTION_H
Thanks guys!
Here's the error message:
include\RationalFraction.h|8|error: expected class-name before '{' token|
include\IrrationalFraction.h|5|error: expected class-name before '{' token|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|