C ++中的用户定义文字运算符错误

问题描述

我正在尝试与文字运算符一起玩,我是一个初学者。我有以下错误代码片段。有人可以帮我找出这3个错误的原因吗?

#include <iostream>
using namespace std;
constexpr float operator "" _km(float);
int main()
{
   auto distance{2.3_km};
   cout<<distance;
   return 0;
}

错误:

3:42: error: 'constexpr float operator""_km(float)' has invalid argument list
 In function 'int main()':
6:22: error: unable to find numeric literal operator 'operator""_km'
6:22: note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes
6:28: error: unable to deduce 'std::initializer_list<auto>' from '{<expression error>}'

解决方法

从第一个错误开始:

constexpr float operator""_km(float)的参数列表无效

这是因为数字文字运算符必须具有参数类型long double。返回float很好。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...