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. Passing a vector as function argument
Qt 6.11 is out! See what's new in the release blog

Passing a vector as function argument

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 471 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.
  • S Offline
    S Offline
    SOrton
    wrote on last edited by
    #1

    Apologies if this is the wrong place but;
    When defining a QVecotr with:

    QVector<double> x(temp),y(temp);
    

    How can I place this into a functions paramater definition?:

    void MainWindow::plotGraph(int selectedIndex, QVector<double> x,y)
    

    As the x,y is causing a problem due to the comma.

    Christian EhrlicherC J.HilkJ Kent-DorfmanK 3 Replies Last reply
    0
    • S SOrton

      Apologies if this is the wrong place but;
      When defining a QVecotr with:

      QVector<double> x(temp),y(temp);
      

      How can I place this into a functions paramater definition?:

      void MainWindow::plotGraph(int selectedIndex, QVector<double> x,y)
      

      As the x,y is causing a problem due to the comma.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @SOrton said in Passing a vector as function argument:

      As the x,y is causing a problem due to the comma.

      Because you don't assign a type for y - in a function call you can't use the shortcut as for a definition and have to properly define all parameters.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • S SOrton

        Apologies if this is the wrong place but;
        When defining a QVecotr with:

        QVector<double> x(temp),y(temp);
        

        How can I place this into a functions paramater definition?:

        void MainWindow::plotGraph(int selectedIndex, QVector<double> x,y)
        

        As the x,y is causing a problem due to the comma.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @SOrton
        void MainWindow::plotGraph(int selectedIndex, QVector<double> x, QVector<double>y);

        preferably
        void MainWindow::plotGraph(int selectedIndex, const QVector<double> &x, const QVector<double> &y);


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • S SOrton

          Apologies if this is the wrong place but;
          When defining a QVecotr with:

          QVector<double> x(temp),y(temp);
          

          How can I place this into a functions paramater definition?:

          void MainWindow::plotGraph(int selectedIndex, QVector<double> x,y)
          

          As the x,y is causing a problem due to the comma.

          Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by Kent-Dorfman
          #4

          @SOrton said in Passing a vector as function argument:

          QVector<double> x(temp),y(temp);

          How can I place this into a functions paramater definition?:
          void MainWindow::plotGraph(int selectedIndex, QVector<double> x,y)

          Ok First thing. You need to create a "using" or "typedef" declaration for your container, as an alias when refering to the type later in your code

          using MY_VEC = QVector<double>;

          MY_VEC v1(27U);
          MY_VEC v2 = { 0.0, 1.0, 37.555 };
          void someFunction(MY_VEC& v) { v.clear(); }
          someFunction(v2);

          Finally, don't put multiple variable declarations on the same line. It's considered bad form by most modern coding standards.

          The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

          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