如何将类成员函数的返回类型设置为私有结构的对象 [英] How to set the Return Type of a Class Member Function as the object of a Private Struct

查看:47
本文介绍了如何将类成员函数的返回类型设置为私有结构的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,标题冗长而混乱,但是我想不出更好的方法来提出这个问题.所以,我有一个班级:

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

当我尝试实现r_add函数时,我不断收到这样的错误:离线定义的返回类型不同于r_add函数的声明中的返回类型.当我尝试在类成员函数中调用私有结构时,我不清楚如何声明返回类型.

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.

推荐答案

该语法必须为:

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

请注意,您不必 Set< T> :: Node * 用作参数类型,因为范围 Set< T> 届时将用于 Node .

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.

另一个选择是使用尾随返回类型.这样可以避免不必要地输入 Set< T> .

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

这篇关于如何将类成员函数的返回类型设置为私有结构的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆