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

孤者浪人 提交于 2021-02-05 09:42:41

问题


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.


回答1:


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



回答2:


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

here is my implementation for map, and you can find the solution to your problem



来源:https://stackoverflow.com/questions/50575836/how-to-set-the-return-type-of-a-class-member-function-as-the-object-of-a-private

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!