可以这样继承吗?我只是得到错误:
class Human:public Node<Human>; //Здесь unknown template name 'Node' и expected '{' after base class list
template <class T>
class Node{
private:
T data;
QList<Node<T>*> childs;
QList<Node<T>*> parents;
public:
Node(){
qDebug( "Node construct" );
};
Node(T* newdata){
qDebug( "Node construct" );
data = *newdata;
};
T get_data(){ return data; }
void set_data(T new_data){data=new_data;}
void add_child(T* child){ childs.push_back(new Node(child));}
void add_parent(T* parent){parents.push_back(new Node(parent));}
QList<Node<T>*>* get_childs(){return &childs;}
QList<Node<T>*>* get_parents(){return &parents;}
~Node(){
}
};
class Human:public Node<Human>{ // А здесь redefinition of 'Human'
private:
int id;
Node<Human> *node = new Node<Human>();
Photo avatar;
QString firstname;
QString lastname;
QString description;
Gallery gallery;
QList<Event*> events;
public:
Human(){}
Human(QString firstname,QString lastname,QString description):firstname(firstname),lastname(lastname),description(description){};
QString get_firstname();
QString get_lastname();
Photo get_avatar();
QList<Event *> get_events();
Gallery get_gallery();
void change_firstname(QString);
void change_lastname(QString);
void change_description(QString);
void add_child(Human*);
void add_parent(Human*);
QList<Node<Human> *> *get_childs();
QList<Node<Human> *> *get_parents();
};
您的代码中有对象的循环嵌套。一个类
Human
包括作为Node<Human>
基础子对象并Node<Human>
包括作为Human
子对象data
。这当然是不可能的。无论您如何在这里旋转,您总是会尝试在不允许不完整类型的上下文中声明不完整类型的对象,或者不可能进行类型推导,或类似的事情。