Is it possible to inherit this way? I just have errors popping up:

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(); }; 
  • Seemingly ordinary CRTP. What caused this question? Does the code work or not? - VTT
  • @VTT yes, does not work - alex-rudenkiy 5:58 pm
  • If it does not work, then the question should be closed, due to the lack of a description of the problem and an example to reproduce it. - VTT
  • @VTT he says that the Node uses the Human type which is not defined, which is logical, but when I prescribe the heading of the Human before Node (the Human class: Public Node <Human>) he swears - alex-rudenkiy
  • one
    What for? Where do you have at least one mention of the Human identifier in the Node class? - AnT

1 answer 1

In your code, there is a circular nesting of objects. The Human class includes the Node<Human> as the base sub-object, and the Node<Human> includes the Human as the sub-object data . This is of course impossible.

No matter how cool you are, you will always rest against trying to declare an object of an incomplete type in a context where an incomplete type is not allowed, or the impossibility of deduction of a type or something else.