string_view 和 basic_string<char> 有什么联系,为什么 string_view 示例代码不起作用?

What's the connection of string_view and basic_string<char> and why does string_view example code not work?

我已经从 Bjarne Stroustrup 的 C++ 之旅 中复制代码来测试字符串视图,但我不断收到错误消息:

error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(std::basic_string_view<char>::size_type

我正在使用 VS Code WSL 2Ubuntu 20.04gcc-11

main.cpp,我有:

#include <iostream>
#include "strings.h"

using namespace std;

int main () {
    string king = "Harold";
    auto s1 = cat(king, "William");
}

strings.h中,我有以下内容。函数是按照教科书中的那样复制的。我输入它是为了避免使用不同编码的特殊 * 字符。

#pragma once
#include <string>

using namespace std;

string cat(string_view sv1, string_view sv2) {
    string res(sv1.length()+sv2.length());
    char* p = &res[0];

    for (char c : sv1)                  // one way
        *p++ = c;
    copy(sv2.begin(), sv2.end(), p);    // another way
    return res;
}

使用 Ctrl+F5 命令会出现此错误。

In file included from main.cpp:2:
strings.h: In function ‘std::string cat(std::string_view, std::string_view)’:
strings.h:7:41: error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(std::basic_string_view<char>::size_type)’
    7 |     string res(sv1.length()+sv2.length());
      |                                         ^
In file included from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:1:
/usr/include/c++/11/bits/basic_string.h:650:9: note: candidate: ‘template<class _Tp, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Tp&, const _Alloc&) [with _Tp = _Tp; <template-parameter-2-2> = <template-parameter-1-2>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
  650 |         basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
      |         ^~~~~~~~~~~~
/usr/include/c++/11/bits/basic_string.h:650:9: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/11/bits/move.h:57,
                 from /usr/include/c++/11/bits/nested_exception.h:40,
                 from /usr/include/c++/11/exception:148,
                 from /usr/include/c++/11/ios:39,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:1:

我把我从Visual Studio代码中看到的内容截屏以表示带下划线的代码,这不会使

我认为它可能是代码编辑器的问题,但 Godbolt 显示了类似的错误。

因为是Vs Code相关的,这里是我的

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++-11 - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++-11 build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++20",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

是的,这似乎是错误的。

string res(sv1.length()+sv2.length());

应该是

string res(sv1.length()+sv2.length(), '[=11=]');

std::string 没有仅接受大小参数的构造函数,与 std::vector 不同。必须明确提供用于初始化元素的值。


不过,我在勘误表 here 中找不到这个错误。