Reputation: 77
I have a class "poly" and a class "node". The class poly is made from a linked list of nodes. I am trying to pass a poly to a function "printPoly" that will allow me to print the linked list of nodes. But I am having trouble accessing the variables of the nodes...
Here is my code:
class Node
{
private:
double coeff;
int exponent;
Node *next;
public:
Node(double c, int e, Node *nodeobjectPtr)
{
coeff = c;
exponent = e;
next = nodeobjectPtr;
}
};
class poly
{
private:
Node *start;
public:
poly(Node *head) /*constructor function*/
{
start = head;
}
void printPoly(); //->Poly *p1 would be the implicit parameter
};
void poly :: printPoly()
{
poly *result = NULL;
result = this;
double c;
int e;
Node *result_pos = res->start; //create ptr to traverse linked nodes
while(result_pos!= NULL)
{
c = result_pos->coeff; // I CANT ACCESS THESE???
e = result_pos->exponent;
printf(....);
result_pos = result_pos->next; //get next node (also can't access "next")
}
I think it has something to do with the fact that "coeff, exponent, and next" are private variables of the node class. But since my poly class is made up of nodes shouldn't it be able to access these?
Upvotes: 0
Views: 58
Reputation:
Private variables and functions in a class can only be accessed by the function inside that class. Anything you want to use from outside of that class (e.g. the way you are now) has to be public.
Upvotes: 2