Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with passing object of my class as parameter to other object.
Qt 6.12 LTS is out! See what's new in the release blog

Problem with passing object of my class as parameter to other object.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 3.3k Views 2 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.
  • C Offline
    C Offline
    cmoud94
    wrote on last edited by cmoud94
    #1

    Hi,

    I'm working on text editor in Qt and I have a small problem. There are 2 classes MainWindow and Editor and what I'm trying to do is to pass one of the as a parameter to the other.

    Here is the LINK to gist containing header files of that 2 classes.

    If I compile this code it gives me two errors:

    • editor.h:13: error: expected ')' before '*' token
    Editor(MainWindow *parent, QString path, QString name, QString content);
    
    • editor.h:23: error: 'MainWindow' does not name a type
    MainWindow *parent;
    

    Thanks for help.

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

      @cmoud94 said:

      MainWindow' does not name a type

      hi, this sounds like you did not include "mainwindow.h"
      so it knows that type?

      C 1 Reply Last reply
      0
      • mrjjM mrjj

        @cmoud94 said:

        MainWindow' does not name a type

        hi, this sounds like you did not include "mainwindow.h"
        so it knows that type?

        C Offline
        C Offline
        cmoud94
        wrote on last edited by
        #3

        @mrjj Hi, thanks for fast reply. Include is not the issue here, if you look at the gist I've posted link to, you'll see that I have "mainwindow.h" included in "editor.h".

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

          You have circular includes: MainWindow includes Editor and Editor includes MainWindows. This is not going to work.
          If you only use pointer to MainWindow in Editor you can remove the include and add forward declaration:

          class MainWindow;
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Don't forget to include MainWindow in Editor.cpp

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • D Offline
              D Offline
              DRoscoe
              wrote on last edited by
              #6

              I was going to suggest the same as @jsulm. Use forward declaration of MainWindow in your header and include the header in your CPP.

              1 Reply Last reply
              2
              • C Offline
                C Offline
                cmoud94
                wrote on last edited by
                #7

                Thanks a lot guys, forward declaration solved it.

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

                  Hi,

                  That circular dependency triggers another question: why do you need it ? Sounds like you are preparing a tight coupling between these two classes and it's something you should avoid.

                  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

                  • Login

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