0
if (polynomial1->get(0)->compareTo(polynomial2->get(0)) == 0)
{
    polynomial1->get(0)->coefficient += polynomial2->get(0)->coefficient;
    result->insert_tail->polynomial1->get(0);
}

Polynomial1 and Polynomial2 are both Linked Lists and I am adding polynomial terms together one node at a time. In my compareTo function if both the terms in the linked lists == 0 then I want access the coefficient and add the coefficient of both terms together. My problem is accessing the coefficient. I keep getting the error message:

class Data has no member named ‘coefficient’

But my PolynomialTerm class inherits Data. Any help on accessing the coefficient?

class PolynomialTerm : public Data
{
    public:
    int coefficient;
    Variable *variable;

    PolynomialTerm(int coefficient, Variable *variable) :
    coefficient(coefficient), variable(variable)
    { }

    int compareTo(Data *other) const
    {
        PolynomialTerm * otherTerm = (PolynomialTerm*)other;

        return variable->variableX == otherTerm->variable->variableX &&
            variable->variableX == otherTerm->variable->variableX &&
            variable->exponentX == otherTerm->variable->exponentX &&
            variable->exponentY == otherTerm->variable->exponentY ? 0 :
            variable->exponentX > otherTerm->variable->exponentX ||
            variable->exponentY > otherTerm->variable->exponentY ? -1 : 1;
    }

---edit--

here is also my Data class which is located in my header file.

class Data {
  public:
    virtual ~Data() {}

    /**
     * Returns 0 if equal to other, -1 if < other, 1 if > other
     */
    virtual int compareTo(Data * other) const = 0;

    /**
    * Returns a string representation of the data
    */
   virtual string toString() const = 0;
};
user2510809
  • 235
  • 3
  • 14

2 Answers2

1

I suppose you're getting error here:

polynomial1->get(0)->coefficient

And (it's again my guess) this is because the get function is defined in base class (Data) and returns a pointer to Data (not PolynomialTerm). And of course Data doesn't have coefficient (only the PolynomialTerm does).

The compiler doesn't know that the pointer returned by get actually points to PolynomialTerm instance. Hence you get the error.

One way of fixing this would be casting the pointer type to it's actual type PolynomialTerm*:

dynamic_cast<PolynomialTerm*>(polynomial1->get(0))->coefficient
nullptr
  • 11,008
  • 1
  • 23
  • 18
  • my get function is actually defined in my linked list class. get returns Data at the index given. each node stores data and in this case a PolynomialTerm which inherits Data. – user2510809 Jul 14 '13 at 18:42
  • Oh, I see. But my suggestion is still valid and should work for you. (Maybe making the linked list class to be a template would be a more elegant solution.) – nullptr Jul 14 '13 at 18:54
  • I tried using your suggestion but then I get this error message: error: cannot dynamic_cast ‘polynomial1-> LinkedList::get(0)’ (of type ‘class Data*’) to type ‘class PolynomialTerm’ (target is not pointer or reference) – user2510809 Jul 14 '13 at 18:58
  • Sorry, I have a typo here. Of course you should cast to `PolynomialTerm*` (pointer type, I've missed `*`). – nullptr Jul 14 '13 at 19:00
0
PolynomialTerm(int coefficient, Variable* variable):
     coefficient(coefficient), variable(variable){}

Your compiler may be confused by coefficient(coefficient). Change either the argument name or the member name:

PolynomialTerm(int coef, Variable* var):
     coefficient(coef), variable(var){}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154