0

我有以下代码:

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. 任何想法为什么?

4

4 回答 4

3

因为它是您当前定义的类型。一个类型拥有相同类型的成员是没有意义的(对于初学者来说,它的大小是无限的)。我认为您想要的是指向's 的指针RandomTreeNode<T>,而不是直接实例。

于 2012-10-22T16:33:27.613 回答
1

您不能在此类中声明类的实例。

在这里您声明RandomTreeNode<T> left;RandomTreeNode<T> right;在您的RandomTreeNode. 因此类型的声明是不完整的。

您应该使用指针RandomTreeNode<T>来避免此错误。

于 2012-10-22T16:36:08.627 回答
0

正确编译的代码(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 定义类型。

于 2012-10-22T17:21:16.733 回答
-1

尝试前向声明。在全局范围内将其写在程序的开头。

template<class T>
class RandomTreeNode ;

它给你错误,因为你正在声明你要声明的类型的变量。

于 2012-10-22T16:34:15.187 回答