c++ - C++17,string + string_view 给出编译错误
问题描述
根据“Nicolai M. Josuttis”的 C++17 书,应该编译以下代码。
#include <string_view>
#include <string>
int main()
{
std::string_view sv1 = "hello";
std::string_view sv2 = "world";
auto s = std::string(sv1) + sv2;
}
我收到以下错误(为清楚起见进行了编辑)
test.cpp: In function ‘int main()’:
test.cpp:7:28: error: no match for ‘operator+’ (operand types are ‘std::string’ and ‘std::string_view’)
7 | auto s = std::string(sv1) + sv2;
| ~~~~~~~~~~~ ^ ~~~
| | |
| | std::string_view {aka std::basic_string_view<char>}
| std::string {aka std::basic_string<char>}
In file included from /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/include/c++/bits/stl_algobase.h:67,
from /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/include/c++/string_view:42,
from test.cpp:1:
解决方案
推荐阅读
- python - 我是给 cross_val_score() 整个数据集还是只给训练集?
- flutter - Flutter StatefulWidget 层次结构
- java - Android Studio 活动场景
- swift - 获取所有项目列表并将其传递给数组
- javascript - Nightwatch.js:如何在运行测试套件之前运行登录脚本
- javascript - 如何在我的网页中移动 dojo 脚本?
- c - 使用 _may_i_use_cpu_feature 时出现“未解决的外部”错误
- javascript - Highcharts:在散点图上反转图例过滤器
- java - Cucumber - cucumber.api.SnippetType 类型无法解析。它是从所需的 .class 文件中间接引用的
- r - scatter3d - 基于第四个变量的颜色点