我想从我的Term结构中返回学位。从我的理解我需要访问Node-> Term-> Degree但是我的函数不会将Node作为参数,那么我该如何处理呢?
//This function returns the degree for example
//a.degree returns degree
int Polynomial::degree() const{
}
struct Term{
int coeff;
int degree;
};
struct Node {
Term *term;
Node *next;
};
这些是我的结构。
Polynomial.cpp如下(缩短):
using namespace std;
struct Term{
int coeff;
int degree;
};
struct Node {
Term *term;
Node *next;
};
int Polynomial::degree() const{
}
Polynomial.h如下(缩写):
using namespace std;
Class Polynomial {
struct Term{
int coeff;
int degree;
};
struct Node {
Term *term;
Node *next;
};
public:
int degree() const;
private:
Node * poly;
答案 0 :(得分:0)
基本上,您可以执行以下操作:
int Polynomial::degree() const{
// Here add your code to check if the pointers are null and
// implement your early return or exception handling strategy.
// Below is how to get the degree value using Node type data member
return poly->term->degree;
}
这只是如何使用poly获得学位。在尝试访问该值之前,请不要忘记无效检查。