首页 > 解决方案 > 错误:function_name 之前的声明符无效

问题描述

我有以下代码来返回按比例缩小的值及其各自的测量单位:

enum class convert_mode {up_metric, low_metric, metric, bit_metric, imperial}

std::pair <double, std::string> auto_range (uint64_t x, int factor1 = 1000, int factor2 = 10){

        double val = (double) x;
        int i = 0;
        while (val >= factor1 ){
                val = val / factor1;
                i++;
                }
        switch (i) {
                case 0 : return std::make_pair( digit_rm( val, factor2 ) ,"_" );
                case 1 : return std::make_pair( digit_rm( val, factor2 ) ,"K" );
                case 2 : return std::make_pair( digit_rm( val, factor2 ) ,"M" );
                case 3 : return std::make_pair( digit_rm( val, factor2 ) ,"G" );
                case 4 : return std::make_pair( digit_rm( val, factor2 ) ,"T" );
                case 5 : return std::make_pair( digit_rm( val, factor2 ) ,"P" );
                }
        
        return std::make_pair(0,"_");
        }

在编译时返回以下错误:

get_info.cpp:41:33: error: invalid declarator before ‘auto_range’
   41 | std::pair <double, std::string> auto_range (uint64_t x, int factor1 = 1000, int factor2 = 10){
      |                                 ^~~~~~~~~~
get_info.cpp: In function ‘int main(int, char**)’:
get_info.cpp:270:14: error: ‘auto_range’ was not declared in this scope
  270 |   auto x1 =  auto_range(PC.get_memory_info(), factor1, factor2);
      |              ^~~~~~~~~~

作为旁注,代码之前确实编译正确,我从以下答案中启发了这个方法:Returning multiple values from a C++ function

编辑:

  1. auto_range ( number_to_reduce, reduction_per_unit, reduction_of_digits )
  2. 返回对<reduced_number, unit_of_measurement >
double digit_rm (double val, int factor = 100){
    return (double)((int)(val*factor))/factor; 
    }

#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string> 
#include <iomanip>  // std::seprecision, std::fixed 
#include <math.h>
#include <sys/statvfs.h>
#include <cstring>
#include <utility>
// See: https://man7.org/linux/man-pages/man2/sysinfo.2.html
#include <sys/sysinfo.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string> 
#include <iomanip>  // std::seprecision, std::fixed 
#include <math.h>
#include <sys/statvfs.h>
#include <cstring>
#include <utility>
// See: https://man7.org/linux/man-pages/man2/sysinfo.2.html
#include <sys/sysinfo.h>

double digit_rm (double val, int factor = 100){
    return (double)((int)(val*factor))/factor; 
    }

enum class convert_mode {up_metric, low_metric, metric, bit_metric, imperial}

std::pair < double, std::string > auto_range (uint64_t x, int factor1 = 1000, int factor2 = 10){

    double val = (double) x;
    int i = 0;
    while (val >= factor1 ){
        val = val / factor1;
        i++;
        }
    switch (i) {
        case 0 : return std::make_pair( digit_rm( val, factor2 ) ,"_" ); 
        case 1 : return std::make_pair( digit_rm( val, factor2 ) ,"K" ); 
        case 2 : return std::make_pair( digit_rm( val, factor2 ) ,"M" ); 
        case 3 : return std::make_pair( digit_rm( val, factor2 ) ,"G" );
        case 4 : return std::make_pair( digit_rm( val, factor2 ) ,"T" );
        case 5 : return std::make_pair( digit_rm( val, factor2 ) ,"P" ); 
        }
    
    return std::make_pair(0,"_");
    }

int main (int argc, char **argv)
{

double x = 1234567;          //say this is Hz
auto y = auto_range(x);
std::cout << y.first << " " << y.second << "Hz" << std::endl;    //Should return 1.2Mhz

return 0;
}

标签: c++functionmethodsstd-pair

解决方案


推荐阅读