Thalia
Thalia

Reputation: 14615

Forward declaration of (sdk) class containing template

I have been doing forward declarations in header files, and including the actual class files in cpp. But I run into problems when the classes are templated:

class MyClass {
public:
  MyClass();
  void aFunction();
private:
  QList<int> m_member;
};

To get it to build I need to give this class info about QList. I tried:

class QList;

error: template argument required for 'class QList'

I tried (because I will only need QList of integers in this particular class):

class QList<int>;

error: specialization of 'QList<int>' after instantiation

I have looked up these errors but found only issues with people having trouble creating class templates, found nothing about forward declarations.

If nothing else works, I can #include <QList> in the header file and give up on forward declaration - but I would like to understand this issue.

This option is also suggested in the most popular question about template classes forward declarations:

Just #include <list> and don't worry about it.

I don't understand other answers...

Upvotes: 2

Views: 337

Answers (1)

imreal
imreal

Reputation: 10378

You can forward declare a templated class, like this:

template<typename>
class QList;

But it wont work if you declare a member with this type (i.e. m_member) that is not a reference or a pointer.

Upvotes: 1

Related Questions