so I am very new to C++ programming so I apologize beforehand if I am asking something trivial. My assignment is to add, multiply and evaluate polynomials where each term of a specified polynomial is represented by a Node class with private variables: double coefficient, int power and Node *next.

class Node{
private:
double coef;
int power;
Node *next;
public: blah
}

The head to that linked list (for each polynomial), is to be stored in an array of Poly objects where the only private variable in my Poly class is Node *head.

class Poly{
private:
Node *head;
public:poly functions;
}

The user is to select the polynomial they want to work with by selecting an element from my polynomial array, and this will give the head to the selected polynomial.

poly_array[n];

However my issue now is that the element of this array is of object Poly and I want to make it of class Node so I can actually extract its contents of the class and use this method to transverse through the nodes of the selected polynomial(s). This is the code I have tried to implement to make this work but my function call of convert poly returns garbage. I am lost as to what method I should try next. Thank you in advance. This is where I try to first transverse a polynomial to display its contents.

void init_polydisplay(vector<Poly*> polynomial_array, int numofpolys)
{
Poly *polyobject;
Node *polyhead;

for (int n = 0; n < numofpolys; n++)
{
    temp3.getnodehead();
    polyhead=polyobject->convertPoly(polynomial_array[n]);
}
}

My attempt at trying to return Node* versus just the head of the polynomial.

Node* Poly::convertPoly(Poly* tmp)
{
return (Node *) tmp;
}
有帮助吗?

解决方案

You can define a get_head() function in Poly

class Poly{
private:
Node *head;
public:
 Node * get_head()
    {
      return head;
    }
};

and use it this way:

polyhead = polynomial_array[n]->get_head();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top