gpt4 book ai didi

c++ - 通用类对象数组 C++

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:43:47 26 4
gpt4 key购买 nike

我有一个通用类 Queue,它包含一个模板 Ttype2 作为将存储在每个节点的信息字段中的数据类型的放置容器。 在我的驱动程序类中,我想实例化一个 Queue 类对象数组,但我似乎无法弄清楚。我该怎么做呢?

这些没有用,但说明了我正在努力完成的事情:

// Queue Complex[] = new Queue();//invalid use of template name without identifier list
//Queue<Ttype2> Complex[]; //template arg 1 is invalid
// vector<Queue> Complex2[];//invalid template arguments`

Queue.h header 中的队列类声明和构造函数:

template <typename Ttype2>
class Queue
{
// Global Data Items
protected:
Node <Ttype2> Front, Rear;
int Length;

// member function prototypes
public:
Queue();
void AddRear(Node <Ttype2> ThisNode);
Node <Ttype2> RemoveFront();
void Modify(int Position, Node <Ttype2> ThisNode);
void ClearAll();
int GetSize();`
Node <Ttype2> GetNode(int Position);
Node <Ttype2>* toArray();
};`

// Constructor
template <typename Ttype2>
Queue <Ttype2> :: Queue()
{
Rear = Front = NULL;
Length = 0;
} // End of Constructor
`

最佳答案

这个有效:

Queue<int> *Complex = new Queue<int>();
Queue<int> Complex[1];
vector<Queue<int>> Complex2[1];

在实例化模板时,您需要为模板提供真实的参数。

Queue<Ttype2> // Ttype2 isn't a real type, use int, char, ...

您还需要定义类型 Node<> .如果你想分配 NULLRearFront它,首先考虑使它们成为指针,其次使用nullptr而不是 NULL .

关于c++ - 通用类对象数组 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43554637/

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