简体   繁体   English

在double struct C ++中返回值

[英]Returning a value inside a double struct C++

I want to return the degree from my Term struct.From my understanding I need to access the Node->Term->Degree however my function doesn't take a Node as a argument so how would I approach this? 我想从我的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;
};

These are my structs. 这些是我的结构。

Polynomial.cpp is as followed (shortened) : Polynomial.cpp如下(缩短):

using namespace std;
struct Term{
   int coeff;
   int degree;
};

struct Node {
   Term *term;
   Node *next;
};

int Polynomial::degree() const{

}

Polynomial.h is as followed (shortened) : 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;

Basically you do the following: 基本上,您可以执行以下操作:

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

This is ONLY how to get the degree using poly. 这只是使用poly获得学位的方法。 Please do not forget the nullity check before trying to access the value. 尝试访问该值之前,请不要忘记无效检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM