Note that there are some explanatory texts on larger screens.

plurals
  1. POaccessing data members in a Linkedlist
    primarykey
    data
    text
    <pre><code>if (polynomial1-&gt;get(0)-&gt;compareTo(polynomial2-&gt;get(0)) == 0) { polynomial1-&gt;get(0)-&gt;coefficient += polynomial2-&gt;get(0)-&gt;coefficient; result-&gt;insert_tail-&gt;polynomial1-&gt;get(0); } </code></pre> <p><code>Polynomial1</code> and <code>Polynomial2</code> 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:</p> <blockquote> <p>class <code>Data</code> has no member named <code>‘coefficient’</code></p> </blockquote> <p>But my <code>PolynomialTerm</code> class inherits <code>Data</code>. Any help on accessing the coefficient?</p> <pre><code>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-&gt;variableX == otherTerm-&gt;variable-&gt;variableX &amp;&amp; variable-&gt;variableX == otherTerm-&gt;variable-&gt;variableX &amp;&amp; variable-&gt;exponentX == otherTerm-&gt;variable-&gt;exponentX &amp;&amp; variable-&gt;exponentY == otherTerm-&gt;variable-&gt;exponentY ? 0 : variable-&gt;exponentX &gt; otherTerm-&gt;variable-&gt;exponentX || variable-&gt;exponentY &gt; otherTerm-&gt;variable-&gt;exponentY ? -1 : 1; } </code></pre> <p>---edit--</p> <p>here is also my Data class which is located in my header file.</p> <pre><code>class Data { public: virtual ~Data() {} /** * Returns 0 if equal to other, -1 if &lt; other, 1 if &gt; other */ virtual int compareTo(Data * other) const = 0; /** * Returns a string representation of the data */ virtual string toString() const = 0; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload