gpt4 book ai didi

c++ - 使用模板类定义声明变量内联

转载 作者:行者123 更新时间:2023-11-30 01:56:51 26 4
gpt4 key购买 nike

我有一个这样的链表结构:

template<class T>
class LinkedList {
private:

template<class U>
struct Node {
U data;
Node<U> *link;
};

Node<T> *head;
};

我最想做的是将内部类定义与 head 的声明合并,如:

// Is something like this possible?
template<class U>
struct Node {
U data;
Node<U> *link;
} head; // Put head right here somehow, while also specifying the type parameter.

自从创建 Node<U> 的整点以来结构就是定义head .是否可以将这两者联系在一起?

最佳答案

只保留占位符 T 并删除不必要的 U:

template<class T>
class LinkedList {
private:

struct Node {
T data;
Node *link;
};

Node *head;
};

关于c++ - 使用模板类定义声明变量内联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19384775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com