Cannot pass vector<float> to the function
-
wrote on 28 Feb 2023, 11:59 last edited by
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.
-
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.
wrote on 28 Feb 2023, 12:05 last edited by JonB@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. -
@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.wrote on 28 Feb 2023, 12:22 last edited by@JonB I knew it was something very silly but didint realise it was that bad! Thank you very much, well spotted.
-
@JonB I knew it was something very silly but didint realise it was that bad! Thank you very much, well spotted.
wrote on 28 Feb 2023, 14:36 last edited by JoeCFD@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/4