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. Cannot pass vector<float> to the function
Forum Update on Monday, May 27th 2025

Cannot pass vector<float> to the function

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 293 Views
  • 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
    lukutis222
    wrote on 28 Feb 2023, 11:59 last edited by
    #1

    Hello. I am trying to pass a float vector (list of float values) to the function. The function calculates the average of all those values in the vector and returns the final value. My function look as following:

    float calculate_1_min_average(uint32_t sample_counter, uint8_t index_1_min,std::vector< float >average_vector){
        int index_limit_1 = 0;
        double final_1_min_avg_sum = 0;
        if(sample_counter % 5 == 0){ // recalculate this only every 5 samples
            if((sample_counter/5) >= 60){
                index_limit_1 = 60;
            }
            else{
                index_limit_1 = (index_1_min+1);
            }
            qDebug("index_limit_1 = %u \n",index_limit_1);
            for (int i = 0; i < index_limit_1; ++i)
            {
                final_1_min_avg_sum += average_vector[i];
            }
            float final_1_min_avg = ((float)final_1_min_avg_sum)/(index_limit_1); //or cast sum to double before division
            return final_1_min_avg;
        }
        return 0;
    }
    

    In the header file, I have function prototype declared:

        float calculate_1_min_average(uint32_t sample_counter, uint8_t index_1_min,std::vector< float >average_vector);
    
    

    The vector that I try to pass to that function is as following:

    std::vector< float > current_1_min_avg; // array that can hold 60 elements.
    

    and I am calling the function:

    float final_1_min_avg = calculate_1_min_average(sample_counter,index_1_min,current_1_min_avg);
    

    I get the following error:

    C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: debug/mainwindow.o: in function `MainWindow::readData()':
    C:\Users\petrikas.lu\Desktop\WORK\QT\uCurrent\build-uCurrent-Desktop_Qt_6_4_0_MinGW_64_bit-Debug/../uCurrent/mainwindow.cpp:211: undefined reference to `MainWindow::calculate_1_min_average(unsigned int, unsigned char, std::vector<float, std::allocator<float> >)'
    collect2.exe: error: ld returned 1 exit status
    

    I cannot wrap my head around what is the problem. I bet its some silly mistake I have made! Could someone help point it out for me please.

    J 1 Reply Last reply 28 Feb 2023, 12:05
    0
    • L lukutis222
      28 Feb 2023, 11:59

      Hello. I am trying to pass a float vector (list of float values) to the function. The function calculates the average of all those values in the vector and returns the final value. My function look as following:

      float calculate_1_min_average(uint32_t sample_counter, uint8_t index_1_min,std::vector< float >average_vector){
          int index_limit_1 = 0;
          double final_1_min_avg_sum = 0;
          if(sample_counter % 5 == 0){ // recalculate this only every 5 samples
              if((sample_counter/5) >= 60){
                  index_limit_1 = 60;
              }
              else{
                  index_limit_1 = (index_1_min+1);
              }
              qDebug("index_limit_1 = %u \n",index_limit_1);
              for (int i = 0; i < index_limit_1; ++i)
              {
                  final_1_min_avg_sum += average_vector[i];
              }
              float final_1_min_avg = ((float)final_1_min_avg_sum)/(index_limit_1); //or cast sum to double before division
              return final_1_min_avg;
          }
          return 0;
      }
      

      In the header file, I have function prototype declared:

          float calculate_1_min_average(uint32_t sample_counter, uint8_t index_1_min,std::vector< float >average_vector);
      
      

      The vector that I try to pass to that function is as following:

      std::vector< float > current_1_min_avg; // array that can hold 60 elements.
      

      and I am calling the function:

      float final_1_min_avg = calculate_1_min_average(sample_counter,index_1_min,current_1_min_avg);
      

      I get the following error:

      C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: debug/mainwindow.o: in function `MainWindow::readData()':
      C:\Users\petrikas.lu\Desktop\WORK\QT\uCurrent\build-uCurrent-Desktop_Qt_6_4_0_MinGW_64_bit-Debug/../uCurrent/mainwindow.cpp:211: undefined reference to `MainWindow::calculate_1_min_average(unsigned int, unsigned char, std::vector<float, std::allocator<float> >)'
      collect2.exe: error: ld returned 1 exit status
      

      I cannot wrap my head around what is the problem. I bet its some silly mistake I have made! Could someone help point it out for me please.

      J Offline
      J Offline
      JonB
      wrote on 28 Feb 2023, 12:05 last edited by JonB
      #2

      @lukutis222 said in Cannot pass vector<float> to the function:

      undefined reference to MainWindow::calculate_1_min_average

      This is a member method from the declaration. Your float calculate_1_min_average is a free function in its definition.

      L 1 Reply Last reply 28 Feb 2023, 12:22
      3
      • J JonB
        28 Feb 2023, 12:05

        @lukutis222 said in Cannot pass vector<float> to the function:

        undefined reference to MainWindow::calculate_1_min_average

        This is a member method from the declaration. Your float calculate_1_min_average is a free function in its definition.

        L Offline
        L Offline
        lukutis222
        wrote on 28 Feb 2023, 12:22 last edited by
        #3

        @JonB I knew it was something very silly but didint realise it was that bad! Thank you very much, well spotted.

        J 1 Reply Last reply 28 Feb 2023, 14:36
        1
        • L lukutis222
          28 Feb 2023, 12:22

          @JonB I knew it was something very silly but didint realise it was that bad! Thank you very much, well spotted.

          J Offline
          J Offline
          JoeCFD
          wrote on 28 Feb 2023, 14:36 last edited by JoeCFD
          #4

          @lukutis222 And it is better to pass your vector as const reference. I guess you do not want to make a copy of it.
          float calculate_1_min_average(uint32_t sample_counter, uint8_t index_1_min, const std::vector< float > & average_vector){

          1 Reply Last reply
          0

          1/4

          28 Feb 2023, 11:59

          • Login

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