[英]Member access into incomplete type error: Templatized struct C++
Pretty new to C++ and having my first try at templates. 对C ++来说很新,我第一次尝试模板。 I created a struct, treeNode
, having left, right and parent pointers. 我创建了一个struct, treeNode
,有左,右和父指针。 I want the tree to be able to store multiple data types and hence am using templates. 我希望树能够存储多种数据类型,因此我正在使用模板。 Whenever I try create an instance of the struct in the .cpp file, and then use it to access the left/right pointers of the tree I get this error: Member access into incomplete type struct 'treeNode'. 每当我尝试在.cpp文件中创建结构的实例,然后使用它来访问树的左/右指针时,我会收到此错误:成员访问不完整类型struct'treeNode'。 Any idea on what I'm missing? 对我缺少什么有任何想法?
Here is the code in the .h file(struct definition): 这是.h文件中的代码(结构定义):
template <class T>
struct treeNode {
node<T> *l;
node<T> *r;
node<T> *p;
};
Here is my attempt in the .cpp file: 这是我在.cpp文件中的尝试:
#include "RedBlack.h"
struct treeNode* t;
Tree::Tree() {
t->l = NULL;
}
struct
, members should have the same type of the struct
itself. 首先,因为您声明了一个递归struct
,所以成员应该具有相同类型的struct
本身。 Tree
class below) 在用真实类型替换类型变量之前,它们不提供真正可用的实现,因此您必须始终使用它们专用或保持某些类型变量仍然在另一个模板上下文中取消(例如,下面的Tree
类) Since you want to have a generic tree, then the Tree
class should be generic too: 由于您希望拥有通用树,因此Tree
类也应该是通用的:
template <class T>
struct node {
node<T> *l;
node<T> *r;
node<T> *p;
};
template <class T>
class Tree
{
private:
node<T> *root;
public:
Tree() : root(nullptr) { }
};
Tree<int> *tree = new Tree<int>();
A treeNode
is nothing -- literally not a thing -- without it's template parameters. 没有它的模板参数, treeNode
什么都不是 - 字面上不是一件事 。
You must do: 你必须这样做:
treeNode <int> * t;
Or something similar. 或类似的东西。
Apart from that you did not set a template argument it seems you made a typo. 除此之外你没有设置模板参数,似乎你写了一个错字。 Instead of 代替
template <class T>
struct treeNode {
node<T> *l;
node<T> *r;
node<T> *p;
};
should be 应该
template <class T>
struct treeNode {
treeNode<T> *l;
treeNodeT> *r;
treeNode<T> *p;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.