C ++:不完整类型

时间:2012-10-22 16:30:38

标签: c++ compiler-errors

我有以下代码:

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 个答案:

答案 0 :(得分:3)

因为它是您当前定义的类型。对于具有相同类型成员的类型没有意义(对于初学者,它将具有无限大小)。我想你想要的是指针RandomTreeNode<T>,而不是直接实例。

答案 1 :(得分:1)

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

您可以在RandomTreeNode<T> left;声明中声明RandomTreeNode<T> right;RandomTreeNode。因此,该类型的声明并不完整。

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

答案 2 :(得分: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定义类型。

答案 3 :(得分:-1)

尝试前进声明。在全球范围内的程序开头写下这个。

template<class T>
class RandomTreeNode ;

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