Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
72 views
in Technique[技术] by (71.8m points)

c++ - How to set the Return Type of a Class Member Function as the object of a Private Struct

sorry for the long and confusing title, but I couldn't think of a better way to ask this. So, what I have is a class:

template <typename T>
class Set
{
    public:
        //random member functions here
    private:
        struct Node{
            T key;
            Node *right;
            Node *left;
            int height;
        };
    public:
        Node* r_add(Node *temp);
};


Node* Set<T>::r_add(Node *temp)
{
    return temp;
}

When I try to implement the function r_add, I keep getting the error that the return type of out-of-line definition differs from that in the declaration for the r_add function. I'm not exactly clear on how to declare the return type for when I try to call a private structure in a class member function.

question from:https://stackoverflow.com/questions/65856496/how-to-return-pointer-to-a-struct

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The syntax for that needs to be:

template <typename T>
Set<T>::Node* Set<T>::r_add(Node *temp)
{
    return temp;
}

Please note that you don't have to use Set<T>::Node* for the argument type since the scope Set<T> will be used for Node at that point.

Another option is to use trailing return type. That will allow you to avoid having to type Set<T> any more than is necessary.

template <typename T>
auto Set<T>::r_add(Node *temp) -> Node*
{
    return temp;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...