首页 > 解决方案 > 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:

标签: c++gccc++17gcc9

解决方案


推荐阅读