[英]C++: incomplete type
I have the following code: 我有以下代码:
template<class T>
class RandomTreeNode {
public:
typedef typename RandomTreeFunction<T>::function_ptr function_ptr;
RandomTreeNode(): left(NULL), right(NULL), threshold(0.0), is_a_leaf(false), data(NULL), function(0){
}
void set_function(function_ptr function){this->function = function;}
function_ptr get_function(){ return this->function;}
void set_threshold(double threshold){this->threshold = threshold;}
double get_threshold(){return threshold;}
void create_left_child(){this->left = RandomTreeNode<T>();}
//returning references so that they can be altered in a recursive tree build algo without excessive copying
RandomTreeNode<T>& get_left_child(){return left;}
void create_right_child(){this->right = RandomTreeNode<T>();}
RandomTreeNode<T>& get_right_child(){return this->right;}
bool is_leaf(){return this->is_a_leaf;}
void mark_as_leaf(){this->is_a_leaf = true;}
const std::vector<T> get_data(){
return data;
}
void set_data(std::vector<T>& data){
this->data = data;
}
private:
RandomTreeNode<T> left;
RandomTreeNode<T> right;
double threshold;
function_ptr function;
std::vector<T> data;
bool is_a_leaf;
};
When I compile, I get the following error: 'RandomTreeNode<T>::left' has incomplete type
. 编译时,出现以下
error: 'RandomTreeNode<T>::left' has incomplete type
。 Any ideas why? 有什么想法吗?
Because it's the type you're currently defining. 因为这是您当前正在定义的类型。 It doesn't make sense for a type to have a member of the same type (for starters, it would have an infinite size).
一个类型具有相同类型的成员是没有意义的(对于初学者来说,它的大小将是无限的)。 I think what you want is to have pointers to
RandomTreeNode<T>
's, not direct instances. 我认为您想要的是具有指向
RandomTreeNode<T>
的指针 ,而不是直接实例。
You can not declare a instance of a class inside of this class. 您不能在此类的内部声明一个类的实例。
Here you declare RandomTreeNode<T> left;
在这里,您
RandomTreeNode<T> left;
声明RandomTreeNode<T> left;
and RandomTreeNode<T> right;
和
RandomTreeNode<T> right;
inside your declaration of RandomTreeNode
. 在您的
RandomTreeNode
声明中。 Therefore the declaration of the type is not complete. 因此,类型的声明不完整。
You should use pointers to RandomTreeNode<T>
to avoid this error. 您应该使用指向
RandomTreeNode<T>
指针来避免此错误。
Code which is compiling properly(http://codepad.org/ltpxM60i) 正确编译的代码(http://codepad.org/ltpxM60i)
Below code is now compile properly 下面的代码现在可以正确编译
**template<class T>
class RandomTreeFunction{
class function_ptr{
};
};**
template<class T>
class RandomTreeNode {
public:
typedef typename RandomTreeFunction<T>::function_ptr function_ptr;
RandomTreeNode(): left(NULL), right(NULL), threshold(0.0), is_a_leaf(false), data(NULL), function(0){
}
void set_function(function_ptr function){this->function = function;}
function_ptr get_function(){ return this->function;}
void set_threshold(double threshold){this->threshold = threshold;}
double get_threshold(){return threshold;}
void create_left_child(){this->left = RandomTreeNode<T>();}
//returning references so that they can be altered in a recursive tree build algo without excessive copying
RandomTreeNode<T>& get_left_child(){return left;}
void create_right_child(){this->right = RandomTreeNode<T>();}
RandomTreeNode<T>& get_right_child(){return this->right;}
bool is_leaf(){return this->is_a_leaf;}
void mark_as_leaf(){this->is_a_leaf = true;}
const std::vector<T> get_data(){
return data;
}
void set_data(std::vector<T>& data){
this->data = data;
}
private:
RandomTreeNode<T> left;
RandomTreeNode<T> right;
double threshold;
function_ptr function;
std::vector<T> data;
bool is_a_leaf;
};
int main(){
return 0;
}
I think function_ptr is not defined 我认为function_ptr没有定义
typedef typename RandomTreeFunction<T>::**function_ptr** function_ptr;
for typename the following are the rules which applied here(Reference:- http://pages.cs.wisc.edu/~driscoll/typename.html ) 对于typename ,以下是在此处应用的规则(参考: -http : //pages.cs.wisc.edu/~driscoll/typename.html )
The rules 规则
typename is prohibited in each of the following scenarios:
Outside of a template definition. (Be aware: an explicit template specialization (more commonly called a total specialization, to contrast with partial specializations) is not itself a template, because there are no missing template parameters! Thus typename is always prohibited in a total specialization.)
Before an unqualified type, like int or my_thingy_t.
When naming a base class. For example, template <class C> class my_class : C::some_base_type { ... }; may not have a typename before C::some_base_type.
In a constructor initialization list.
typename is mandatory before a qualified, dependent name which refers to a type (unless that name is naming a base class, or in an initialization list).
typename is optional in other scenarios. (In other words, it is optional before a qualified but non-dependent name used within a template, except again when naming a base class or in an initialization list.)
So you might have to define type for function_ptr. 因此,您可能必须为function_ptr定义类型。
Try the forward declaration. 尝试转发声明。 Write this at the start of your program at global scope.
在程序的全局范围开始时编写此代码。
template<class T>
class RandomTreeNode ;
It is giving you error because you are declaring variable of the type you are going to declare. 它给您错误,因为您正在声明要声明的类型的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.