在双结构 C++ 中返回一个值

Returning a value inside a double struct C++

我想 return 我的学期的学位 struct.From 我的理解是我需要访问 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;

基本上你会做以下事情:

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;
}

这只是使用多边形获得学位的方法。请不要忘记在尝试访问该值之前进行无效检查。