如何解决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
很好。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。