gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:53:43 37 4
gpt4 key购买 nike

我已经从 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/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com