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. Returning struct from class method

Returning struct from class method

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 326 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 last edited by lukutis222
    #1

    Hello. In my application I want to return 3 values from a class method. For that, I have decided to create a struct (final_average_s) that contains the 3 values that I want to return.

    The method that should return those 3 values is Calculate_every_1_min and is also declared below:

    Inside my MainWindow.h , I declare the struct and the method

    private:
        Ui::MainWindow *ui;
    
        struct final_average_s{
            float minute_30;
            float minute_60;
            float hour_24;
        };
    
        final_average_s Calculate_every_1_min(uint32_t sample_counter_1_sec,float avg_1_min);
    

    Inside MainWindow.cpp the method Calculate_every_1_min is defined:

    final_average_s MainWindow::Calculate_every_1_min(uint32_t sample_counter_1_sec,float avg_1_min){
        final_average_s final_average; // create local struct variable to return it
        index_30_min = ((sample_counter_1_sec/60)%30); // index 30 min will consists of 30 1 minute samples
        qDebug("index 30 min = %u \n",index_30_min);
    
    
        index_60_min = ((sample_counter_1_sec/60)%60); // index 60 min will consists of 60 1 minute samples
        qDebug("index 60 min = %u \n",index_60_min);
    
        index_24_hr = ((sample_counter_1_sec/60)%1440); // index 60 min will consists of 60 1 minute samples
        qDebug("index 24 hr = %u \n",index_24_hr);
    
    
        current_30_min_avg[index_30_min-1] = avg_1_min;
        current_60_min_avg[index_60_min-1] = avg_1_min;
        current_24_hr_avg[index_24_hr-1] = avg_1_min;
    
        final_average.minute_30 = calculate_30_min_average(sample_counter,index_30_min,current_30_min_avg);
        ui->minute_30_edit->setText(QString::number(final_average.minute_30));
    
        final_average.minute_60 = calculate_60_min_average(sample_counter,index_60_min,current_60_min_avg);
        ui->minute_30_edit->setText(QString::number(final_average.minute_60));
    
        final_average.hour_24 = calculate_24_hr_average(sample_counter,index_24_hr,current_24_hr_avg);
        ui->minute_30_edit->setText(QString::number(final_average.hour_24));
    
        return final_average;
    

    And then this function is being called:

    final_average_s final_average = Calculate_every_1_min(sample_counter_1_sec,avg_1_min);
    

    Everything seems okay to me but I am getting the following issues:

    
    
    ../uCurrent/mainwindow.cpp: At global scope:
    ../uCurrent/mainwindow.cpp:836:1: error: 'final_average_s' does not name a type
      836 | final_average_s MainWindow::Calculate_every_1_min(uint32_t sample_counter_1_sec,float avg_1_min){
          | ^~~~~~~~~~~~~~~
    mingw32-make[1]: *** [Makefile.Debug:2827: debug/mainwindow.o] Error 1
    mingw32-make[1]: *** Waiting for unfinished jobs....
    mingw32-make[1]: Leaving directory 'C:/Users/petrikas.lu/Desktop/WORK/QT/uCurrent/build-uCurrent-Desktop_Qt_6_4_0_MinGW_64_bit-Debug'
    mingw32-make: *** [Makefile:45: debug] Error 2
    08:28:56: The process "C:\Qt\Tools\mingw1120_64\bin\mingw32-make.exe" exited with code 2.
    Error while building/deploying project uCurrent (kit: Desktop Qt 6.4.0 MinGW 64-bit)
    When executing step "Make"
    

    I do not quite understand what the compiler means by saying final_average_s does not name a type. Any help is appreciated!

    jsulmJ 1 Reply Last reply
    0
    • L lukutis222

      Hello. In my application I want to return 3 values from a class method. For that, I have decided to create a struct (final_average_s) that contains the 3 values that I want to return.

      The method that should return those 3 values is Calculate_every_1_min and is also declared below:

      Inside my MainWindow.h , I declare the struct and the method

      private:
          Ui::MainWindow *ui;
      
          struct final_average_s{
              float minute_30;
              float minute_60;
              float hour_24;
          };
      
          final_average_s Calculate_every_1_min(uint32_t sample_counter_1_sec,float avg_1_min);
      

      Inside MainWindow.cpp the method Calculate_every_1_min is defined:

      final_average_s MainWindow::Calculate_every_1_min(uint32_t sample_counter_1_sec,float avg_1_min){
          final_average_s final_average; // create local struct variable to return it
          index_30_min = ((sample_counter_1_sec/60)%30); // index 30 min will consists of 30 1 minute samples
          qDebug("index 30 min = %u \n",index_30_min);
      
      
          index_60_min = ((sample_counter_1_sec/60)%60); // index 60 min will consists of 60 1 minute samples
          qDebug("index 60 min = %u \n",index_60_min);
      
          index_24_hr = ((sample_counter_1_sec/60)%1440); // index 60 min will consists of 60 1 minute samples
          qDebug("index 24 hr = %u \n",index_24_hr);
      
      
          current_30_min_avg[index_30_min-1] = avg_1_min;
          current_60_min_avg[index_60_min-1] = avg_1_min;
          current_24_hr_avg[index_24_hr-1] = avg_1_min;
      
          final_average.minute_30 = calculate_30_min_average(sample_counter,index_30_min,current_30_min_avg);
          ui->minute_30_edit->setText(QString::number(final_average.minute_30));
      
          final_average.minute_60 = calculate_60_min_average(sample_counter,index_60_min,current_60_min_avg);
          ui->minute_30_edit->setText(QString::number(final_average.minute_60));
      
          final_average.hour_24 = calculate_24_hr_average(sample_counter,index_24_hr,current_24_hr_avg);
          ui->minute_30_edit->setText(QString::number(final_average.hour_24));
      
          return final_average;
      

      And then this function is being called:

      final_average_s final_average = Calculate_every_1_min(sample_counter_1_sec,avg_1_min);
      

      Everything seems okay to me but I am getting the following issues:

      
      
      ../uCurrent/mainwindow.cpp: At global scope:
      ../uCurrent/mainwindow.cpp:836:1: error: 'final_average_s' does not name a type
        836 | final_average_s MainWindow::Calculate_every_1_min(uint32_t sample_counter_1_sec,float avg_1_min){
            | ^~~~~~~~~~~~~~~
      mingw32-make[1]: *** [Makefile.Debug:2827: debug/mainwindow.o] Error 1
      mingw32-make[1]: *** Waiting for unfinished jobs....
      mingw32-make[1]: Leaving directory 'C:/Users/petrikas.lu/Desktop/WORK/QT/uCurrent/build-uCurrent-Desktop_Qt_6_4_0_MinGW_64_bit-Debug'
      mingw32-make: *** [Makefile:45: debug] Error 2
      08:28:56: The process "C:\Qt\Tools\mingw1120_64\bin\mingw32-make.exe" exited with code 2.
      Error while building/deploying project uCurrent (kit: Desktop Qt 6.4.0 MinGW 64-bit)
      When executing step "Make"
      

      I do not quite understand what the compiler means by saying final_average_s does not name a type. Any help is appreciated!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @lukutis222 said in Returning struct from class method:

      final_average_s MainWindow::Calculate_every_1_min(uint32_t sample_counter_1_sec,float avg_1_min){

      Change to:

      MainWindow::final_average_s MainWindow::Calculate_every_1_min(uint32_t sample_counter_1_sec,float avg_1_min){
      

      else compiler does not know what final_average_s is because it is defined inside MainWindow.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      3
      • jsulmJ jsulm

        @lukutis222 said in Returning struct from class method:

        final_average_s MainWindow::Calculate_every_1_min(uint32_t sample_counter_1_sec,float avg_1_min){

        Change to:

        MainWindow::final_average_s MainWindow::Calculate_every_1_min(uint32_t sample_counter_1_sec,float avg_1_min){
        

        else compiler does not know what final_average_s is because it is defined inside MainWindow.

        L Offline
        L Offline
        lukutis222
        wrote on last edited by
        #3

        @jsulm said in Returning struct from class method:

        MainWindow::final_average_s

        Issue fixed. Thank you very much

        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