0

I'm developing an application using Qt Widgets.

In my header files, I like to forward declare classes instead of including them. At the beginning of a header file I put forward declarations as follows:

class QFile;
class QDate;
class QTime;

And, there is a function declaration in the class as the following:

static bool addGoldTransaction(QFile *transactionsFile, Gold goldType, OperationType transactionType, float price, float amount, QDate date, QTime time);

When I try to compile, it gives an error like the following:

forward declaration of 'class QDate'
in definition of macro 'QT_FORWARD_DECLARE_STATIC_TYPES_ITER'
in expansion of macro 'QT_FOR_EACH_STATIC_CORE_CLASS'
In file included from moc_transaction.cpp:9:0:
error: initializing argument 6 of 'static bool addGoldTransaction(QFile*, Gold, OperationType, float, float, QDate, QTime)'

There is no error for forward declarations of other Qt-related classes.

Including the QDate header file solves the issue but I wonder:

  1. Why does the compiler complain only about QDate class while it doesn't complain about other classes? Is there anything special with QDate class related to this issue?
  2. How can I forward declare QDate class?
ukll
  • 204
  • 2
  • 12
  • 4
    The 6th parameter of `addGoldTransaction` takes a `QDate` by value so it's full definition must be known by then so it can be copied (ie know it's size). A forward declaration just introduces a name it says nothing about the size of the object. – Richard Critten Dec 23 '17 at 01:40
  • The 7th parameter of `addGoldTransaction` takes a `QTime` by value too. But it doesn't complain about anything. What is special with `QDate` class? – ukll Dec 23 '17 at 01:48

1 Answers1

0

Pass 6 and 7 arguments of types QDate and QTime by references QDate& and QTime& or better by const references const QDate& and const QTime&.

273K
  • 29,503
  • 10
  • 41
  • 64
  • Yes, it solves the issue. But why does the compiler does not complain about the 7th argument (QTime) while it is complaining about the 6th one (QDate) when I declare the function as in the question? – ukll Dec 23 '17 at 01:59
  • 1
    I assume the compiler reports the first error occurrence in one line. If you fix only the 6th argument, then the compiler reports new error in the 7th argument. – 273K Dec 23 '17 at 02:02
  • Yeah, I've just tried that. It solves the issue. I now can use forward declaration for both QDate and QTime. – ukll Dec 23 '17 at 02:06