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 Offline
    Q Offline
    Q139
    wrote on 6 Nov 2015, 12:59 last edited by Q139 11 Jun 2015, 13:03
    #1

    How to initialize class with pointer to class where object to is located;

    For example Inside class A is created class B object, now class A can com to class B via object but for class B to com to class A would it be possible to initialize class with pointer or there are better solutions?

    ´´´
    class FileCOM : public QObject
    {

    public:
    FileCOM(TCP_Socketptr); <<<------ this attempt fails and compiler returns : error: expected ')' before '' token

    void set_file_location();
    void read_file_to_buffer();
    void process_buffer();
    void write_file(QByteArray data);
    std::vector <QByteArray> read_buffer;
    

    };
    ´´´

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 6 Nov 2015, 13:06 last edited by mrjj 11 Jun 2015, 13:07
      #2

      just create a constructor that accepts such pointer ?

      class FileCOM : public QObject
      {
      TCP_Socket *mykeptpointer;
      public:
      FileCOM(TCP_Socket * theptr) : mykeptpointer(theptr) {}
      ...
      or do i misunderstand you ?

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Q139
        wrote on 6 Nov 2015, 13:18 last edited by Q139 11 Jun 2015, 13:19
        #3

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

        M 1 Reply Last reply 6 Nov 2015, 13:22
        0
        • Q Q139
          6 Nov 2015, 13:18

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 6 Nov 2015, 13:22 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 6 Nov 2015, 13:43
          0
          • M mrjj
            6 Nov 2015, 13:22

            @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 6 Nov 2015, 13:43 last edited by Q139 11 Jun 2015, 13:47
            #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?

            M 2 Replies Last reply 6 Nov 2015, 13:47
            0
            • Q Q139
              6 Nov 2015, 13:43

              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?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 6 Nov 2015, 13:47 last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • Q Q139
                6 Nov 2015, 13:43

                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?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 6 Nov 2015, 13:50 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 6 Nov 2015, 13:58 last edited by Q139 11 Jun 2015, 14:00
                  #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

                  M 1 Reply Last reply 6 Nov 2015, 14:00
                  0
                  • Q Q139
                    6 Nov 2015, 13:58

                    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

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 6 Nov 2015, 14:00 last edited by mrjj 11 Jun 2015, 14:02
                    #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 6 Nov 2015, 14:03 last edited by Q139 11 Jun 2015, 14:04
                      #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

                      M 1 Reply Last reply 6 Nov 2015, 14:05
                      0
                      • Q Q139
                        6 Nov 2015, 14:03

                        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

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 6 Nov 2015, 14:05 last edited by mrjj 11 Jun 2015, 14:05
                        #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 6 Nov 2015, 14:06 last edited by
                          #12

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

                          M 1 Reply Last reply 6 Nov 2015, 14:07
                          0
                          • Q Q139
                            6 Nov 2015, 14:06

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

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 6 Nov 2015, 14:07 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

                            M 1 Reply Last reply 6 Nov 2015, 14:13
                            0
                            • M mrjj
                              6 Nov 2015, 14:07

                              @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

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 6 Nov 2015, 14:13 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 6 Nov 2015, 14:31 last edited by Q139 11 Jun 2015, 14:37
                                #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
                                • M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 6 Nov 2015, 14:36 last edited by
                                  #16

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

                                  1 Reply Last reply
                                  0

                                  6/16

                                  6 Nov 2015, 13:47

                                  10 unread
                                  • Login

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