c++ - 错误: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
编辑:
- 该功能应该像这样工作:
- auto_range ( number_to_reduce, reduction_per_unit, reduction_of_digits )
- 返回对<reduced_number, unit_of_measurement >
- digit_rm 是一个删除逗号后剩余数字的函数(12345m => 12.345Km => digit_rm(12.345,10) => 12.3Km)。
double digit_rm (double val, int factor = 100){
return (double)((int)(val*factor))/factor;
}
- 在这种情况下可能
#include
是:
#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>
如果这很重要,我正在使用 Linux 机器(Ubuntu 20.04.2 LTS x86_64)。
这
enum class
是未来修改的一部分,如果值得一提的话。这是一个例子:
#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;
}
解决方案
推荐阅读
- microsoft-graph-api - 如何将 onedrive 文件共享给组织外的人员
- go - 如何使用gorm创建到mysql的ssl连接?
- angularjs - 使用 angularjs 显示 API 内容
- linux - rc.local 的环境
- python - 无法用尽加载更多按钮来揭开所有头条新闻
- c - 拆分字符串并使用索引来调用所需的元素
- swift - 与 collectionView.reloadItems[at: [indexPath]] 重叠标签值
- scheme - 在 do 循环中填充列表返回列表为空
- java - android studio 如何解决 无法解决:play-services-basement?
- powershell - 将 CSV 文件日期时间字段从 UTC 更新为本地时间的 Power shell 脚本