我有一个类“poly”和一个类“node”。类 poly 由节点的链接列表组成。我正在尝试将一个多边形传递给一个函数“printPoly”,该函数将允许我打印节点的链接列表。但是我无法访问节点的变量...
这是我的代码:
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")
}
我认为这与“coeff、指数和next”是节点类的私有(private)变量有关。但是由于我的 poly 类是由节点组成的,它不应该能够访问这些节点吗?
最佳答案
类中的私有(private)变量和函数只能被该类内部的函数访问。 任何你想在该类(class)之外使用的东西(例如你现在的样子)都必须公开。
关于c++ - 访问类变量时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23185265/