Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Friend class using base class
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Friend class using base class

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Max13
    wrote on last edited by
    #1

    hi, I have two classes using each other:

    @// Message.hpp

    include "StatusController.hpp"

    class Message : public QObject
    {
    Q_OBJECT
    friend class StatusController;

    public: enum MessageType {
        Unknown,
        Discover,
        Alive,
        KeepAlive,
        Bye,
        Received,
        Confirmed,
        Answer,
        Text,
    };
    
    private:
        quint32     m_id;
        MessageType m_type;
        Service     m_sender;
        QString     m_content;
    

    };@

    @// StatusController.php

    include "Message.hpp"

    class Message;

    class StatusController : public QObject
    {
    Q_OBJECT
    private:
    QQueue<Message> m_statusList;

    public:
        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.

    We all have started by asking questions. Then after some time, we can begin answering them.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Forward class declaration are used for pointers variable only.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Max13
        wrote on last edited by
        #3

        [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:
        @Message::MessageTye
        ^~~~~@

        We all have started by asking questions. Then after some time, we can begin answering them.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Max13
            wrote on last edited by
            #5

            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...

            We all have started by asking questions. Then after some time, we can begin answering them.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Max13
                wrote on last edited by
                #7

                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. !

                We all have started by asking questions. Then after some time, we can begin answering them.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved