Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Passing a class (pointer) to library - Linux

Passing a class (pointer) to library - Linux

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondiscussionlinuxdebugging
5 Posts 2 Posters 0 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I have one more question. I will try my best to describe the issue and hoping the discussion does not get centered on terminology and RTFM as in the past. That is not an instruction on how to reply, just a polite request to get real and skip opinions. ( And if you think me saying this is rude... I am just trying to get of the dime and writing it as politely as I know) I did have resolved the syntax of "passing the class" , however now I am stuck with WHERE the class should be declared. In layman terms - the calling object passes a class pointer to the function, which happens to be my own created library. I expect the library function to PROCESS the passed class... And that is where I am not sure if I have to define / declare the SAME class again - this time in the library. I did test this using / passing SINGLE variable pointer and it worked OK. I am not sure what to do when the entire class is passed. I do not think dupicating the class definition is vise. (Maybe it needs to be declared as global to the project ?) Any theoretical help , answer, would be appreciated, I can manage the code... Cheers

    class BT_UTILITY_LIBRARY_EXPORT BT_Utility_Library
    {
    public:
    // class variable
    // forward declaration ? works here !
    class Test;

    //May31
    QString DEBUG\_Process(void);
    QString DEBUG\_Process(int \*);
    QString DEBUG\_Process(class Test \*);
    QString DEBUG\_Process\_Class(class Test \*);
    
    QString REG\_EXP\_Process(void);
    
    class Test
    {
       public:
       int a;
       //int b\[a\];
       QString text;
    }TestPointer;
    
    K 1 Reply Last reply
    0
    • L Lost User

      I have one more question. I will try my best to describe the issue and hoping the discussion does not get centered on terminology and RTFM as in the past. That is not an instruction on how to reply, just a polite request to get real and skip opinions. ( And if you think me saying this is rude... I am just trying to get of the dime and writing it as politely as I know) I did have resolved the syntax of "passing the class" , however now I am stuck with WHERE the class should be declared. In layman terms - the calling object passes a class pointer to the function, which happens to be my own created library. I expect the library function to PROCESS the passed class... And that is where I am not sure if I have to define / declare the SAME class again - this time in the library. I did test this using / passing SINGLE variable pointer and it worked OK. I am not sure what to do when the entire class is passed. I do not think dupicating the class definition is vise. (Maybe it needs to be declared as global to the project ?) Any theoretical help , answer, would be appreciated, I can manage the code... Cheers

      class BT_UTILITY_LIBRARY_EXPORT BT_Utility_Library
      {
      public:
      // class variable
      // forward declaration ? works here !
      class Test;

      //May31
      QString DEBUG\_Process(void);
      QString DEBUG\_Process(int \*);
      QString DEBUG\_Process(class Test \*);
      QString DEBUG\_Process\_Class(class Test \*);
      
      QString REG\_EXP\_Process(void);
      
      class Test
      {
         public:
         int a;
         //int b\[a\];
         QString text;
      }TestPointer;
      
      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      It sounds like you're trying to implement in interface. Typically you'd do this by determining what behaviors an object might require and write a base (abstract) class that captures that. You then write your library functions so that they only make use of the public functions in the base class. The derived objects implement the methods in the base class in a way that makes sense for the derived object. There's plenty of examples out there, just google for "C++ interface" or "C++ abstract class". Almost any of which will give you better examples that I can come up with.

      "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

      L 1 Reply Last reply
      0
      • K k5054

        It sounds like you're trying to implement in interface. Typically you'd do this by determining what behaviors an object might require and write a base (abstract) class that captures that. You then write your library functions so that they only make use of the public functions in the base class. The derived objects implement the methods in the base class in a way that makes sense for the derived object. There's plenty of examples out there, just google for "C++ interface" or "C++ abstract class". Almost any of which will give you better examples that I can come up with.

        "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Could somebody please help me with solving this? note: passing argument to parameter 'Agrument' here QString DEBUG_Process_Class(class REG_EXP_Class *Agrument ); I just cannot find any RTFM to "passing class pointer" to function , and I have no issues passing int * to a function. Thanks for the reply. I am not sure HOW to change to the "abstract class". I have been struggling with Qt implementation of anything related to inheritance. Anyway, I did implemented the "supporting " class as a global variable. I have not done "globals" for years and it was an OF memory exercise...

        // initilize class
        pREG_EXP_Class = new REG_EXP_Class;
        pREG_EXP_Class->a = 10;
        pREG_EXP_Class->text = "test text ";
        // test run
        pBTUL->DEBUG_Process_Class(pREG_EXP_Class);
        // declaration
        // QString DEBUG_Process_Class(class REG_EXP_Class *Argument );

        All is working , until I did try to actually pass the class pointer... Then I get this error and have no ideas where is my coding error.

        /mnt/A_BT_DEC10/A_MAY_9_MAY31_CLEAN_BACKUP/A_APR9_MAR7_MAR19_CLEAN/A_BT_LIBRARY/A_DEC17_Bluetoothctl_Dialog_V1/mainwindow_bluewtoothctl_dialog.cpp:8911:

        error: cannot initialize a parameter of type 'class REG_EXP_Class *' with an lvalue of type 'REG_EXP_Class *'

        mainwindow_bluewtoothctl_dialog.cpp:8911:28: error: cannot initialize a parameter of type 'class REG_EXP_Class *' with an lvalue of type 'REG_EXP_Class *'

        pBTUL->DEBUG_Process_Class(pREG_EXP_Class);

                               ^~~~~~~~~~~~~~
        

        ../A_DEC17_BT_Utility_Library/bt_utility_library.h:59:54: note: passing argument
        to parameter 'Agrument' here
        QString DEBUG_Process_Class(class REG_EXP_Class *Agrument );
        ^

        I could use some advise how to correctly pass the class pointer, this "try this ... try that" is tedious. and before I get " get a book..." I have no idea under what subject to look for... " passing paramater to argument " ???

        L 2 Replies Last reply
        0
        • L Lost User

          Could somebody please help me with solving this? note: passing argument to parameter 'Agrument' here QString DEBUG_Process_Class(class REG_EXP_Class *Agrument ); I just cannot find any RTFM to "passing class pointer" to function , and I have no issues passing int * to a function. Thanks for the reply. I am not sure HOW to change to the "abstract class". I have been struggling with Qt implementation of anything related to inheritance. Anyway, I did implemented the "supporting " class as a global variable. I have not done "globals" for years and it was an OF memory exercise...

          // initilize class
          pREG_EXP_Class = new REG_EXP_Class;
          pREG_EXP_Class->a = 10;
          pREG_EXP_Class->text = "test text ";
          // test run
          pBTUL->DEBUG_Process_Class(pREG_EXP_Class);
          // declaration
          // QString DEBUG_Process_Class(class REG_EXP_Class *Argument );

          All is working , until I did try to actually pass the class pointer... Then I get this error and have no ideas where is my coding error.

          /mnt/A_BT_DEC10/A_MAY_9_MAY31_CLEAN_BACKUP/A_APR9_MAR7_MAR19_CLEAN/A_BT_LIBRARY/A_DEC17_Bluetoothctl_Dialog_V1/mainwindow_bluewtoothctl_dialog.cpp:8911:

          error: cannot initialize a parameter of type 'class REG_EXP_Class *' with an lvalue of type 'REG_EXP_Class *'

          mainwindow_bluewtoothctl_dialog.cpp:8911:28: error: cannot initialize a parameter of type 'class REG_EXP_Class *' with an lvalue of type 'REG_EXP_Class *'

          pBTUL->DEBUG_Process_Class(pREG_EXP_Class);

                                 ^~~~~~~~~~~~~~
          

          ../A_DEC17_BT_Utility_Library/bt_utility_library.h:59:54: note: passing argument
          to parameter 'Agrument' here
          QString DEBUG_Process_Class(class REG_EXP_Class *Agrument );
          ^

          I could use some advise how to correctly pass the class pointer, this "try this ... try that" is tedious. and before I get " get a book..." I have no idea under what subject to look for... " passing paramater to argument " ???

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          QString DEBUG_Process_Class(class REG_EXP_Class *Agrument );

          Remove the word class from the parameter in your function definition. The type REG_EXP_Class* is complete, it tells the compiler that Agrument is a pointer to an object of the REG_EXP_Class class. I would also suggest you remove the word Class from your class names, as it is redundant.

          1 Reply Last reply
          0
          • L Lost User

            Could somebody please help me with solving this? note: passing argument to parameter 'Agrument' here QString DEBUG_Process_Class(class REG_EXP_Class *Agrument ); I just cannot find any RTFM to "passing class pointer" to function , and I have no issues passing int * to a function. Thanks for the reply. I am not sure HOW to change to the "abstract class". I have been struggling with Qt implementation of anything related to inheritance. Anyway, I did implemented the "supporting " class as a global variable. I have not done "globals" for years and it was an OF memory exercise...

            // initilize class
            pREG_EXP_Class = new REG_EXP_Class;
            pREG_EXP_Class->a = 10;
            pREG_EXP_Class->text = "test text ";
            // test run
            pBTUL->DEBUG_Process_Class(pREG_EXP_Class);
            // declaration
            // QString DEBUG_Process_Class(class REG_EXP_Class *Argument );

            All is working , until I did try to actually pass the class pointer... Then I get this error and have no ideas where is my coding error.

            /mnt/A_BT_DEC10/A_MAY_9_MAY31_CLEAN_BACKUP/A_APR9_MAR7_MAR19_CLEAN/A_BT_LIBRARY/A_DEC17_Bluetoothctl_Dialog_V1/mainwindow_bluewtoothctl_dialog.cpp:8911:

            error: cannot initialize a parameter of type 'class REG_EXP_Class *' with an lvalue of type 'REG_EXP_Class *'

            mainwindow_bluewtoothctl_dialog.cpp:8911:28: error: cannot initialize a parameter of type 'class REG_EXP_Class *' with an lvalue of type 'REG_EXP_Class *'

            pBTUL->DEBUG_Process_Class(pREG_EXP_Class);

                                   ^~~~~~~~~~~~~~
            

            ../A_DEC17_BT_Utility_Library/bt_utility_library.h:59:54: note: passing argument
            to parameter 'Agrument' here
            QString DEBUG_Process_Class(class REG_EXP_Class *Agrument );
            ^

            I could use some advise how to correctly pass the class pointer, this "try this ... try that" is tedious. and before I get " get a book..." I have no idea under what subject to look for... " passing paramater to argument " ???

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I explained what you should do to correct this in my previous reply. Passing a class pointer or reference is just the same as passing any other type. So a simple example:

            // a useless class
            class Foo
            {
            private :
            int bar;

            public:
            Foo(int i) : bar(i) {} // constructor takes a value
            int getBar() {return bar; } // this method returns the value
            };

            // an arbitrary function which accepts a Foo*
            int MyFunc(Foo* param)
            {
            int rc = param->getBar(); // ask the class object for the value
            return rc * 2; // and return it doubled
            }

            int main
            {
            Foo* pFoo = new Foo(4); // create a new object with a value of 4
            int ans = MyFunc(pFoo); // call MyFunc passing it the pointer to the class object
            cout << "result: " << ans << endl;

            return 0;
            

            }

            And all of that, and more, is fully explained in any C++ reference, either online or in printed copy.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups