Accessing data in a derived classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tex08
    New Member
    • Oct 2008
    • 7

    Accessing data in a derived classes

    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:

    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'
    Here is the code that I believe is relevant to the problem:
    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;
    
    }
    LITERAL.H & LITERAL.CC
    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;
    };
    OP_NEGATE.H & OP_NEGATE.CC
    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;};
    The problem lies in c->GetRight()->GetValue() statement, in attempt to access the derived class' data.

    Thank you in advance for any suggestions.

    Tex
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    c->GetRight()->GetValue()
    Op_Negate::GetR ight() does not return a pointer, so you can't use the arrow operator on it. Mabe you mean
    c->GetRight().Get Value()
    or maybe you want Op_Negate::GetR ight() to return the BooleanExp pointer &_right?
    Last edited by boxfish; Oct 5 '08, 12:19 AM. Reason: Fixing stuff.

    Comment

    • Tex08
      New Member
      • Oct 2008
      • 7

      #3
      My ultimate goal is to represent a boolean expression as a 'tree' using the Composite Pattern. I will need access to the item _right in order to implement an evaluation function via the visitor pattern.

      So I need to figure out a way to access the _right item (there will be other classes that contain left and right elements for other operands). I need to access the derived classes private data to access the values stored there. Example:

      c->GetRight()->GetValue()
      ~~~~~~~~~~ ---> trying to access the object returned by GetRight(), via the GetValue function for that class.
      Last edited by Tex08; Oct 5 '08, 12:50 AM. Reason: Clarification

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        There should be no problem accessing the Op_Negate class's private data through an accessor function like GetRight. That's not what the error is about. Your error says you can't use the arrow operator on something that's not a pointer. Either use the dot operator, or make GetRight return a pointer to _right, &_right, right?

        Comment

        Working...