简体   繁体   中英

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 . 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.

You can not declare a instance of a class inside of this class.

Here you declare RandomTreeNode<T> left; and RandomTreeNode<T> right; inside your declaration of RandomTreeNode . Therefore the declaration of the type is not complete.

You should use pointers to RandomTreeNode<T> to avoid this error.

Code which is compiling properly(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

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 )

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM