For example, I have two class templates, each of which has one or more pointers to another:

Class object

#include "Link.h" #include <QDebug> template <class ValueType> class Object { public: Object() {} ValueType value() const { return _value; } void setValue(const ValueType &value) { _value = value; } void print() const { qDebug() << "object:" << value().toString(); } void link(const Object<ValueType> *other) { Link *ln = new Link(); ln->setFromObject(this); ln->setToObject(other); _links.append(ln); } private: ValueType _value; QList<Link*> _links; }; 

Link class

 #ifndef LINK_H #define LINK_H #include "Object.h" //упс template <class ValueType> class Link { public: Link() {} Object<ValueType> *fromObject() const { return _fromObject; } void setFromObject(Object<ValueType> *fromObject) { _fromObject = fromObject; } Object<ValueType> *toObject() const { return _toObject; } void setToObject(Object<ValueType> *toObject) { _toObject = toObject; } private: Object<ValueType> *_fromObject; Object<ValueType> *_toObject; }; 

Make class declarations (as for "normal" classes - not templates) of the type:

 template <class T> class Object<T>; 

does not allow syntax.

What should be done in such a case (without a large number of frills)?

For example, you can use one of the classes as an argument to the template of another.

 template <class ValueType, class LinkType> class Object { LinkType<ValueType> ... 

then Link

 template <class ValueType> class Link { ... Object<ValueType, Link<ValueType> *_someObject; 

But it is not clear which type should have an argument of another, since they are equal. Is this a “classic” way? There are other solutions?

    1 answer 1

    And if so:

     template<class T> class Object; 

    ?

    • ABOUT! Everything is so simple! :))) - asianirish