简体   繁体   English

如何编写一个返回仅存在于类中的类型的成员函数?

[英]How do I write a member function that returns a type that only exists in the class?

I'm actually implementing a doubly-linked list in C++. 我实际上是在C ++中实现一个双向链接列表。

Here's a MWE of sorts: 这是各种MWE:

namespace mynamespace {

template <typename T>
class List {
public:
    List();

    void prepend(T);
    void append(T);
    void remove(T);

private:
    struct Node {
        T value_;
        Node * prev_;
        Node * next_;
    };

private:
    Node * find(T); // <-- THIS IS MY PROBLEM

private:
    Node * head_;
    Node * tail_;
};

}

The reason I'd like to create that function is because I figured it'd be handy if I could traverse the list with a function like that until I find a given element (I'll need to do the same with the remove() function anyways) 我想创建该函数的原因是因为我认为,如果可以使用这样的函数遍历列表,直到找到给定的元素,这将非常方便(我需要对remove()相同的操作仍然起作用)

But how do I define that function outside the class definition? 但是,如何在class定义之外定义该函数?

Since Node is a private member of the List class, this is not working: 由于NodeList类的私有成员,因此无法正常工作:

template <typename T>
Node * List<T>::find(T val)
{
    // stuff
}

I suppose defining the function inside the class definition would work, because Node makes sense there... Would that be the proper way? 我想定义的内部函数class的定义会的工作,因为Node有意义那里...那是要正确的方法是什么? Even if so, I suppose there must be a way to define the function the way I'm trying to... 即使这样,我想也必须有一种方法可以定义我要尝试的功能...

Since Node is a private member of the List class, this is not working: 由于NodeList类的私有成员,因此无法正常工作:

Actually, that's not correct. 实际上,这是不正确的。 It's not failing because Node is private, but because Node is nested inside of List . 这样做不是因为Node是私有的,而是因为Node嵌套在List The name of the Node class isn't Node , it's List<T>::Node . Node类的名称不是Node ,而是List<T>::Node However, since Node depends on T , you have to write typename List<T>::Node , otherwise the compiler assumes that List<T>::Node is a value rather than a type. 但是,由于Node 依赖于T ,因此您必须编写typename List<T>::Node ,否则编译器会假定List<T>::Node是值而不是类型。 See this question for more information. 有关更多信息,请参见此问题

In short, replace this: 简而言之,替换为:

 template <typename T> Node * List<T>::find(T val) 

With this: 有了这个:

template <typename T>
typename List<T>::Node * List<T>::find(T val)

Alternatively, as StoryTeller noted, if you are in the context of the List<T> class, you can just use Node . 另外,如StoryTeller所述,如果您位于List<T>类的上下文中,则可以仅使用Node You can get in this context by using a trailing return type: 您可以通过使用尾随返回类型来获得此上下文:

template <typename T>
auto List<T>::find(T val) -> Node *

暂无
暂无

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

相关问题 如何在类中创建一个成员函数,该成员函数返回的值是该类对象的类型? - How do I create a member function in a class that returns a value that is the type of an object of that class? 如何创建只在其类型具有特定成员函数时才编译的类? - How do I make a class that only compiles when its type has a certain member function? 如何在返回嵌入类型的泛型类之外实现成员函数? - How to implement a member function outside the generic class that returns an embedded type? 如何用std :: function编写指向成员函数的指针? - How do i write a pointer-to-member-function with std::function? 我如何获得一个类成员函数来访问另一个类成员函数的私有成员? - How do i get a class member function to have access to another class member function's private member? 你称之为成员函数的成员函数是什么?如何编写成员函数? - What do you call a member function that follows a member function and how do I write one? 如何检查类中是否存在成员名称(变量或函数),无论是否指定类型? - How to check if a member name (variable or function) exists in a class, with or without specifying type? 我可以编写一个返回函数的函数类型吗? - Can I write a function type that returns a function? 如何从另一个函数调用属于类成员的函数? - How do I call a function that is member of a class, from another function? 如何获得泛型类成员函数的函数指针? - How do I obtain a function pointer for a generic class member function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM