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. Pass a function with parameter

Pass a function with parameter

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++
6 Posts 2 Posters 1 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

    Reason for this post is I am trying to learn not just "cut and paste". In the following code I am trying to put a function with parameter into "addAction " function It fails with two errors I am trying to decipher and learn what I am doing wrong. I tested

    Run_DDD_OPTION(testWidget); and it runs fine

    QWidget \*testWidget = new QWidget();
    testWidget->setWindowTitle("test widget window ");
    Run\_DDD\_OPTION(testWidget); runs fine 
    
    QAction \*CAT\_DDD\_OPTION = CATMenu->
    addAction(tr("&Device Discovery Dialog (modified Bluetooth scanner ) "),
    this,
    &MainWindow::Run\_DDD\_OPTION(testWidget)
    );
    

    this runs as expected when no parameters are passed

    QAction \*CAT\_DDD\_OPTION\_TAB = CATMenu->
    addAction(tr("&Device Discovery Dialog (modified Bluetooth scanner ) "),
    this,
    &MainWindow::Run\_TAB
    );
    

    There are too many "addAction" functions , different objects, so all I have to go by are these errors. (It woudl probably help if I knew what "addAction" expects as parameters.

    /media/qe/TSET_QT_LABEL/QT_PROGRAMS/JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp:1157: error: lvalue required as unary ‘&’ operand
    ../../../JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp: In member function ‘void MainWindow::createActions()’:
    ../../../JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp:1157:32: error: lvalue required as unary ‘&’ operand
    1157 | &MainWindow::Run_DDD_OPTION(testWidget)
    | ~~~~~~~~~~~~~~~~^~

    /media/qe/TSET_QT_LABEL/QT_PROGRAMS/JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp:1155: error: cannot take the address of an rvalue of type 'void'

    Greg UtasG 1 Reply Last reply
    0
    • L Lost User

      Reason for this post is I am trying to learn not just "cut and paste". In the following code I am trying to put a function with parameter into "addAction " function It fails with two errors I am trying to decipher and learn what I am doing wrong. I tested

      Run_DDD_OPTION(testWidget); and it runs fine

      QWidget \*testWidget = new QWidget();
      testWidget->setWindowTitle("test widget window ");
      Run\_DDD\_OPTION(testWidget); runs fine 
      
      QAction \*CAT\_DDD\_OPTION = CATMenu->
      addAction(tr("&Device Discovery Dialog (modified Bluetooth scanner ) "),
      this,
      &MainWindow::Run\_DDD\_OPTION(testWidget)
      );
      

      this runs as expected when no parameters are passed

      QAction \*CAT\_DDD\_OPTION\_TAB = CATMenu->
      addAction(tr("&Device Discovery Dialog (modified Bluetooth scanner ) "),
      this,
      &MainWindow::Run\_TAB
      );
      

      There are too many "addAction" functions , different objects, so all I have to go by are these errors. (It woudl probably help if I knew what "addAction" expects as parameters.

      /media/qe/TSET_QT_LABEL/QT_PROGRAMS/JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp:1157: error: lvalue required as unary ‘&’ operand
      ../../../JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp: In member function ‘void MainWindow::createActions()’:
      ../../../JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp:1157:32: error: lvalue required as unary ‘&’ operand
      1157 | &MainWindow::Run_DDD_OPTION(testWidget)
      | ~~~~~~~~~~~~~~~~^~

      /media/qe/TSET_QT_LABEL/QT_PROGRAMS/JUNE 15 WORKCOPY 2/CAT/mdi/mainwindow.cpp:1155: error: cannot take the address of an rvalue of type 'void'

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #2

      This looks suspicious to me:

      &MainWindow::Run_DDD_OPTION(testWidget)

      It looks like you're trying to create a pointer to whatever Run_DDD_OPTION returns. However, you also say

      Run_DDD_OPTION(testWidget); // runs fine

      which either means that it returns void or you didn't care what it returned. My guess is the former, which would explain all the error messages. If it returns void, you can't take its address, and there is no lvalue (=a memory address). EDIT: If you want to pass the function Run_DDD_OPTION as an argument to addAction, just write

      addAction(tr("&Device Discovery Dialog(modified Bluetooth scanner)"),
      this,
      MainWindow::Run_DDD_OPTION
      );

      You may need the & in front of the function name--I haven't done this in a long time, so I have to look it up. --It looks like it's optional. If it doesn't work without it, try it with it.

      Robust Services Core | Software Techniques for Lemmings | Articles
      The fox knows many things, but the hedgehog knows one big thing.

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      L 1 Reply Last reply
      0
      • Greg UtasG Greg Utas

        This looks suspicious to me:

        &MainWindow::Run_DDD_OPTION(testWidget)

        It looks like you're trying to create a pointer to whatever Run_DDD_OPTION returns. However, you also say

        Run_DDD_OPTION(testWidget); // runs fine

        which either means that it returns void or you didn't care what it returned. My guess is the former, which would explain all the error messages. If it returns void, you can't take its address, and there is no lvalue (=a memory address). EDIT: If you want to pass the function Run_DDD_OPTION as an argument to addAction, just write

        addAction(tr("&Device Discovery Dialog(modified Bluetooth scanner)"),
        this,
        MainWindow::Run_DDD_OPTION
        );

        You may need the & in front of the function name--I haven't done this in a long time, so I have to look it up. --It looks like it's optional. If it doesn't work without it, try it with it.

        Robust Services Core | Software Techniques for Lemmings | Articles
        The fox knows many things, but the hedgehog knows one big thing.

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

        Thanks I think you may have answered my question. The line is just to test validity of the function.

        Run_DDD_OPTION(testWidget); // runs fine

        addAction(tr("&Device Discovery Dialog(modified Bluetooth scanner)"),
        this,
        MainWindow::Run_DDD_OPTION
        );

        The above code works IF "&" is prefixed , HOWEVER I like to make that parameter of "addAction" variable, more versatile. And that is why I am having issues, hence my post.

        &MainWindow::Run_DDD_OPTION

        So my question would be if this works without parameter why do I need to return a pointer ? I presume it would be same as the passed parmeter type.

        Run_DDD_OPTION

        Now again , I just do not want to "cut and paste" without knowing why. Thanks for your contribution.

        Greg UtasG 1 Reply Last reply
        0
        • L Lost User

          Thanks I think you may have answered my question. The line is just to test validity of the function.

          Run_DDD_OPTION(testWidget); // runs fine

          addAction(tr("&Device Discovery Dialog(modified Bluetooth scanner)"),
          this,
          MainWindow::Run_DDD_OPTION
          );

          The above code works IF "&" is prefixed , HOWEVER I like to make that parameter of "addAction" variable, more versatile. And that is why I am having issues, hence my post.

          &MainWindow::Run_DDD_OPTION

          So my question would be if this works without parameter why do I need to return a pointer ? I presume it would be same as the passed parmeter type.

          Run_DDD_OPTION

          Now again , I just do not want to "cut and paste" without knowing why. Thanks for your contribution.

          Greg UtasG Offline
          Greg UtasG Offline
          Greg Utas
          wrote on last edited by
          #4

          I don't understand how you want to make addAction's last parameter "more versatile". You'll have to explain that.

          Robust Services Core | Software Techniques for Lemmings | Articles
          The fox knows many things, but the hedgehog knows one big thing.

          <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
          <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

          L 1 Reply Last reply
          0
          • Greg UtasG Greg Utas

            I don't understand how you want to make addAction's last parameter "more versatile". You'll have to explain that.

            Robust Services Core | Software Techniques for Lemmings | Articles
            The fox knows many things, but the hedgehog knows one big thing.

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

            Simple, the app will use several GUI interfaces - in Qt terminology "widgets". There is lots of commonality and I like to reuse my code. So passing "widget" as a parameter to addAction would be nice. Besides - I need to get over this "passing function as a parameter with parameters ". I have a feeling QT makes its own syntax... Should that be function ( passFunction, passFunctionparamater ); // pointer , pointer

            Greg UtasG 1 Reply Last reply
            0
            • L Lost User

              Simple, the app will use several GUI interfaces - in Qt terminology "widgets". There is lots of commonality and I like to reuse my code. So passing "widget" as a parameter to addAction would be nice. Besides - I need to get over this "passing function as a parameter with parameters ". I have a feeling QT makes its own syntax... Should that be function ( passFunction, passFunctionparamater ); // pointer , pointer

              Greg UtasG Offline
              Greg UtasG Offline
              Greg Utas
              wrote on last edited by
              #6

              I haven't used Qt, so I don't know if I can help. It sounds like a widget is an object, but you're only passing functions and parameters. Would passing an object help? It's even possible to pass a class's member function[^] as a parameter. I've not used this capability, so you'd have to read up on it if that's what you want. The problem with passing a function and its parameters is that different functions take different parameters. To deal with that, I think you'd need std::function[^]. Again, that's not something I've used, but now you can look for more details. My go-to site for C++ is here[^]. It's a bit formal, so sometimes you need to look elsewhere for explanations and examples that are easier to understand. But it will provide you with the right terminology to use when investigating something.

              Robust Services Core | Software Techniques for Lemmings | Articles
              The fox knows many things, but the hedgehog knows one big thing.

              <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
              <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

              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