Again, I am trying to implement a composite pattern to handle a binary expression. Compiling with g++ (required), I receive the following error trying to access data in a derived class:
Here is the code that I believe is relevant to the problem:
MAIN.CC
LITERAL.H & LITERAL.CC
OP_NEGATE.H & OP_NEGATE.CC
The problem lies in c->GetRight()->GetValue() statement, in attempt to access the derived class' data.
Thank you in advance for any suggestions.
Tex
Code:
make g++ -c main.cc main.cc: In function `int main()': main.cc:23: error: base operand of `->' has non-pointer type `BooleanExp' *** Error code 1 make: Fatal error: Command failed for target `main.o'
MAIN.CC
Code:
// Second attempt
#include <iostream>
#include "BooleanExp.h"
#include "Literal.h"
#include "Variable.h"
#include "Op_Negate.h"
using namespace std;
int main() {
BooleanExp * a, * b, * c;
a = new Literal(false);
b = new Variable("S", true);
c = new Op_Negate( *a );
cout << "Value of a: " << a->GetValue() << endl;
cout << "Name of b: " << b->GetName() << endl;
cout << "Value of b: " << b->GetValue() << endl;
cout << "Value of Negate: " << c->GetRight()->GetValue() << endl;
cout << "So far so good\n";
delete a, b, c;
}
Code:
// Will be Literal.h
#ifndef LITERAL_H
#define LITERAL_H
#include "BooleanExp.h"
class Literal : public BooleanExp {
public:
~Literal() {}
Literal( bool );
bool GetValue();
private:
bool _value;
};
#endif
Code:
// Will be Literal.cc
#include <iostream>
#include "Literal.h"
using namespace std;
Literal::Literal(bool v) {
cout << "In Literal constructor\n";
_value = v;
cout << "Liter Constructor _value =" << _value << endl;
};
bool Literal::GetValue() {
return _value;
};
Code:
// Will be Op_Negate.h
#ifndef OP_NEGATE_H
#define OP_NEGATE_H
#include<iostream>
#include "BooleanExp.h"
using namespace std;
class Op_Negate : public BooleanExp {
public:
~Op_Negate() {}
Op_Negate( BooleanExp );
virtual BooleanExp GetLeft();
virtual BooleanExp GetRight();
private:
BooleanExp _right;
};
#endif
Code:
// Op_Negate.cc
#include <iostream>
#include "Op_Negate.h"
#include "BooleanExp.h"
using namespace std;
Op_Negate::Op_Negate(BooleanExp x) {
cout << "Op_Negate constructor\n";
_right = x;
};
BooleanExp Op_Negate::GetLeft() {};
BooleanExp Op_Negate::GetRight() {return _right;};
Thank you in advance for any suggestions.
Tex
Comment