我有以下代码:
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;
};当我编译时,我得到了下面的error: 'RandomTreeNode<T>::left' has incomplete type。你知道为什么吗?
发布于 2012-10-23 00:33:28
因为它是您当前定义的类型。一个类型有一个相同类型的成员是没有意义的(对于初学者来说,它的大小是无限的)。我认为你想要的是指向RandomTreeNode<T>的指针,而不是直接的实例。
发布于 2012-10-23 00:36:09
不能在此类内声明类的实例。
在这里,您可以在RandomTreeNode声明中声明RandomTreeNode<T> left;和RandomTreeNode<T> right;。因此,类型的声明是不完整的。
您应该使用指向RandomTreeNode<T>的指针来避免此错误。
发布于 2012-10-23 01:21:17
正在正确编译的代码(http://codepad.org/ltpxM60i)
现在可以正确编译以下代码
**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;
}我认为function_ptr没有定义
typedef typename RandomTreeFunction<T>::**function_ptr** function_ptr;对于typename,以下是适用于此处的规则(参考:- http://pages.cs.wisc.edu/~driscoll/typename.html)
规则
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.)因此您可能需要为function_ptr定义类型。
https://stackoverflow.com/questions/13015708
复制相似问题