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

标签 c++ windows-subsystem-for-linux c++20 wsl-2 string-view

我已经从 Bjarne Stroustrup 的 A Tour of C++ 中复制代码来测试字符串 View ,但我不断收到错误:

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 代码中看到的内容的屏幕截图,以指示带下划线的代码,这不会使

Image view of the error in VS Code

我认为它可能是代码编辑器,但 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(), '\0');

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


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

关于c++ - string_view 和 basic_string<char> 有什么联系,为什么 string_view 示例代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71059931/

相关文章:

windows - 如何使用 127.0.0.1 从 Windows 主机访问在 WSL2 中运行的服务?

c++ - 为什么 C++20 中的 time_of_day 和 hh_mm_ss 是两种不同的类型?

c++ - 为什么 div、ldiv 和 lldiv 不是模板?

c++ - 如何在 C++ 中使用 TWAIN 设置扫描的 DPI

java - 在 Linux 的 Windows 子系统上安装 Oracle JDK

c++ - 如何让 vim 对齐三元组? : operator nicely?

python - conda environment : does each new conda environment needs a new kernel to work? 如何为我的所有环境提供特定的库?

c++ - 我可以在C++ 20的类型别名中使用条件吗?

c++ - 只截图窗口的特定区域

c++ - 重写 Java 的 String.toUpperCase() 返回空字符数组