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. How to initialize class with pointer to class
Forum Updated to NodeBB v4.3 + New Features

How to initialize class with pointer to class

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 2 Posters 3.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.
  • Q Q139

    Compiler returns this error msg : error: expected ')' before ' * ' token

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @mrjj said:

    are u sure it knows TCP_Socket ?

    you did include the .h file where it lives ?

    Q 1 Reply Last reply
    0
    • mrjjM mrjj

      @mrjj said:

      are u sure it knows TCP_Socket ?

      you did include the .h file where it lives ?

      Q Offline
      Q Offline
      Q139
      wrote on last edited by Q139
      #5

      Yes included , problem was due to double include in 2 files.
      Is it possible to include file only for .h inside use so when other files include same .h part of files would not be double included?

      mrjjM 2 Replies Last reply
      0
      • Q Q139

        Yes included , problem was due to double include in 2 files.
        Is it possible to include file only for .h inside use so when other files include same .h part of files would not be double included?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #6
        This post is deleted!
        1 Reply Last reply
        0
        • Q Q139

          Yes included , problem was due to double include in 2 files.
          Is it possible to include file only for .h inside use so when other files include same .h part of files would not be double included?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #7

          @Q139
          it is normal to include .h many times.

          Are you using guards ?

          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          #endif

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Q139
            wrote on last edited by Q139
            #8

            For example when include file1.h in file2.h and file2.h in file1.h compiler returns error: expected ')' before '' token FileCOM(TCP_SocketSOCKETptr); or mostly error: 'FileCOM' does not name a type

            For pointer of class A in class B it need include class A .h in B .h file, but for class A to create object of B in .h file it need include of B .h file

            mrjjM 1 Reply Last reply
            0
            • Q Q139

              For example when include file1.h in file2.h and file2.h in file1.h compiler returns error: expected ')' before '' token FileCOM(TCP_SocketSOCKETptr); or mostly error: 'FileCOM' does not name a type

              For pointer of class A in class B it need include class A .h in B .h file, but for class A to create object of B in .h file it need include of B .h file

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #9

              @Q139

              Ah, that is called circular include and often solved by using type forwards.
              https://en.wikipedia.org/wiki/Circular_dependency

              like
              in your case
              class TCP_Socket; //forward declaration

              class FileCOM : public QObject
              {
              TCP_Socket *mykeptpointer;
              ....

              and then DONT include its .h file

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                Q139
                wrote on last edited by Q139
                #10

                Then it is not possible to declare class FileCOM in .h file of TCP_Socket only .cpp file
                Thanks for your time and help, problem is there are multiple instances of sockets and each one should have fileCOM

                mrjjM 1 Reply Last reply
                0
                • Q Q139

                  Then it is not possible to declare class FileCOM in .h file of TCP_Socket only .cpp file
                  Thanks for your time and help, problem is there are multiple instances of sockets and each one should have fileCOM

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #11

                  @Q139
                  well you should never have data in the .h file.

                  but you can using external;
                  in .h
                  extern TCP_Socket * TheOnlyInstance;

                  and in .cpp
                  TCP_Socket * TheOnlyInstance;

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Q139
                    wrote on last edited by
                    #12

                    Thanks , will read materials on circular dependency as have had many problem with this.

                    mrjjM 1 Reply Last reply
                    0
                    • Q Q139

                      Thanks , will read materials on circular dependency as have had many problem with this.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #13

                      @Q139
                      well we all have :)

                      Well in your case:
                      using external.

                      then in mainwindow, u include TCP_Socket.H

                      then you have the instance to give to the other objects when you create them

                      mrjjM 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @Q139
                        well we all have :)

                        Well in your case:
                        using external.

                        then in mainwindow, u include TCP_Socket.H

                        then you have the instance to give to the other objects when you create them

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @Q139 said:

                        there are multiple instances of sockets and each one should have fileCOM

                        the same file com?

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          Q139
                          wrote on last edited by Q139
                          #15

                          Yes same class but different filenames.
                          Foward declaration is good solution and compiles , if it work good , appears to be complex solution for easy problem.

                          .h file

                          class TCP_Socket;

                          class FileCOM : public QObject
                          {
                          public:
                          TCP_Socket * SOCKETptr;
                          FileCOM(TCP_Socket * ptr){
                          SOCKETptr=ptr;
                          }

                          .h file

                          class TCP_Socket : public QObject
                          {
                          public:
                          FileCOM * FILE;

                          .cpp file

                          void TCP_Server_File::NewConnection()
                          {
                          TCP_Socket *soc = new TCP_Socket;
                          soc->FILE = new FileCOM(soc); <--- if working then here it should pointer of TCP_Socket class to FileCOM class.
                          soc->New_socket (server->nextPendingConnection ());
                          mainwin_ptr->AskForNewConnection (soc);
                          Client_count++;
                          }

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

                            it seems fine.
                            SOCKETptr should be the object u expect.

                            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