gpt4 book ai didi

C++ 模板 : create an object of a typename same as of an existing object

转载 作者:行者123 更新时间:2023-11-28 03:28:20 27 4
gpt4 key购买 nike

我正在使用 C++ 中的模板类。我创建了一个类的对象,如下所示:

Node<int> *rootNode = (Node<int> *) malloc(sizeof(Node<int>));

现在我在节点中插入了几个条目。在我看到节点已满后,我希望代码创建一个与根节点具有相同类型名称的新节点并存储所需的数据。下面是我的插入方法:

template <typename T>
RC Node<T>::Insert(void *key)
{
if(space() > 0) { // check if current node has ample space
// add data in the current node
}
else
{
siblingNode = new Node<T>();
if (this->Split(siblingNode, key)) {
if (siblingNode != NULL) {
siblingNode.display();
}
}
}
}
}

我尝试显示使用

创建的新节点

siblingNode.display()

方法,但它给我编译错误

request for member ‘display’ in ‘siblingNode’, which is of non-class type ‘Node<int>*’

如何确保 siblingNode 与调用插入函数的节点的类型名称相同?

最佳答案

siblingNode是一个指针,所以需要使用指针成员解引用操作符:

siblingNode->display()

错误是告诉你你要取消引用的类型是一个指针,而不是你需要有与 Node<T> 相同的类型名。 .

关于C++ 模板 : create an object of a typename same as of an existing object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13319106/

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