Important: Please read the Qt Code of Conduct -

[SOLVED] Friend class using base class

  • hi, I have two classes using each other:

    @// Message.hpp

    include "StatusController.hpp"

    class Message : public QObject
    friend class StatusController;

    public: enum MessageType {
        quint32     m_id;
        MessageType m_type;
        Service     m_sender;
        QString     m_content;


    @// StatusController.php

    include "Message.hpp"

    class Message;

    class StatusController : public QObject
    QQueue<Message> m_statusList;

        StatusController(Message::MessageType type, QObject *parent = 0);


    The forward declaration of Message is because the compilation failed because of the QQueue line.
    Now the compilation fails (error: incomplete type 'Message' named in nested name specifier) on the constructor line.

    I understand that the compiler can't know Message::* AND the friend class inside, at the same time. Is there a way to achieve what I'm trying to do?

    Thanks for your help.

  • Lifetime Qt Champion


    It's because you are using a QQueue<Message> rather than QQueue<Message *>.

    Forward class declaration are used for pointers variable only.

  • [quote author="SGaist" date="1408917856"]It's because you are using a QQueue<Message> rather than QQueue<Message *>.[/quote]

    Thanks for the info, but my problem isn't there. The error is on the contructor line:

  • Lifetime Qt Champion

    You need to include the header if you want to use an enum from a class.

  • Look at line 3 of "StatusController.php", it's included.
    The forward declaration removed the compilation error about QQueue, but the enum is undefined.

    Which is logical because StatusController needs Message to be defined, which needs StatusController to be at least declared, etc...

  • Lifetime Qt Champion

    You don't need to include anything to declare a friend. You need it when you are actually using that friend class so generally in the cpp file.

  • Oh GOOD !

    I've never used friends, I didn't know... Removing the inclusion made disappear ALL the errors:

    • Enum not know
    • QQueue<Message> (anyway, Qt's containers are storing pointers)
    • Forward declaration not needed (it removed some errors, but useless now).

    Thank you for that, I wouldn't have thought about inclusion not needed. !

Log in to reply