her*_*ity 3 c++ string-view windows-subsystem-for-linux c++20 wsl-2
我已经从 Bjarne Stroustrup 的C++ 之旅中复制了代码来测试字符串视图,但我不断收到错误:
\nerror: no matching function for call to \xe2\x80\x98std::__cxx11::basic_string<char>::basic_string(std::basic_string_view<char>::size_type\nRun Code Online (Sandbox Code Playgroud)\n我正在使用 VS CodeWSL 2和Ubuntu 20.04gcc-11
在 中main.cpp,我有:
#include <iostream>\n#include "strings.h"\n\nusing namespace std;\n\nint main () {\n string king = "Harold";\n auto s1 = cat(king, "William");\n}\nRun Code Online (Sandbox Code Playgroud)\n中strings.h,我有以下内容。该函数按照教科书上的内容复制。我把它打出来是为了避免*出现具有不同编码的特殊字符。
#pragma once\n#include <string>\n\nusing namespace std;\n\nstring cat(string_view sv1, string_view sv2) {\n string res(sv1.length()+sv2.length());\n char* p = &res[0];\n\n for (char c : sv1) // one way\n *p++ = c;\n copy(sv2.begin(), sv2.end(), p); // another way\n return res;\n}\nRun Code Online (Sandbox Code Playgroud)\n使用该Ctrl+F5命令会出现此错误。
In file included from main.cpp:2:\nstrings.h: In function \xe2\x80\x98std::string cat(std::string_view, std::string_view)\xe2\x80\x99:\nstrings.h:7:41: error: no matching function for call to \xe2\x80\x98std::__cxx11::basic_string<char>::basic_string(std::basic_string_view<char>::size_type)\xe2\x80\x99\n 7 | string res(sv1.length()+sv2.length());\n | ^\nIn file included from /usr/include/c++/11/string:55,\n from /usr/include/c++/11/bits/locale_classes.h:40,\n from /usr/include/c++/11/bits/ios_base.h:41,\n from /usr/include/c++/11/ios:42,\n from /usr/include/c++/11/ostream:38,\n from /usr/include/c++/11/iostream:39,\n from main.cpp:1:\n/usr/include/c++/11/bits/basic_string.h:650:9: note: candidate: \xe2\x80\x98template<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>]\xe2\x80\x99\n 650 | basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())\n | ^~~~~~~~~~~~\n/usr/include/c++/11/bits/basic_string.h:650:9: note: template argument deduction/substitution failed:\nIn file included from /usr/include/c++/11/bits/move.h:57,\n from /usr/include/c++/11/bits/nested_exception.h:40,\n from /usr/include/c++/11/exception:148,\n from /usr/include/c++/11/ios:39,\n from /usr/include/c++/11/ostream:38,\n from /usr/include/c++/11/iostream:39,\n from main.cpp:1:\nRun Code Online (Sandbox Code Playgroud)\n我截取了从 Visual Studio 代码中看到的内容以指示带下划线的代码,这并不会使
\n\n我认为这可能是代码编辑器的问题,但Godbolt显示了类似的错误。
\n由于这与 Vs Code 有关,这里是我的
\nlaunch.json
{\n // Use IntelliSense to learn about possible attributes.\n // Hover to view descriptions of existing attributes.\n // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387\n "version": "0.2.0",\n "configurations": [\n {\n "name": "g++-11 - Build and debug active file",\n "type": "cppdbg",\n "request": "launch",\n "program": "${fileDirname}/${fileBasenameNoExtension}",\n "args": [],\n "stopAtEntry": false,\n "cwd": "${fileDirname}",\n "environment": [],\n "externalConsole": false,\n "MIMode": "gdb",\n "setupCommands": [\n {\n "description": "Enable pretty-printing for gdb",\n "text": "-enable-pretty-printing",\n "ignoreFailures": true\n },\n {\n "description": "Set Disassembly Flavor to Intel",\n "text": "-gdb-set disassembly-flavor intel",\n "ignoreFailures": true\n }\n ],\n "preLaunchTask": "C/C++: g++-11 build active file",\n "miDebuggerPath": "/usr/bin/gdb"\n }\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n和c_cpp_properties.json
{\n "configurations": [\n {\n "name": "Linux",\n "includePath": [\n "${workspaceFolder}/**"\n ],\n "defines": [],\n "compilerPath": "/usr/bin/gcc",\n "cStandard": "gnu17",\n "cppStandard": "gnu++20",\n "intelliSenseMode": "linux-gcc-x64"\n }\n ],\n "version": 4\n}\nRun Code Online (Sandbox Code Playgroud)\n
是的,这似乎是错误的。
线路
string res(sv1.length()+sv2.length());
Run Code Online (Sandbox Code Playgroud)
应该
string res(sv1.length()+sv2.length(), '\0');
Run Code Online (Sandbox Code Playgroud)
std::string与 相比,它没有仅接受大小参数的构造函数std::vector。必须显式提供用于初始化元素的值。
不过,我在这里的勘误表中找不到这个错误。
| 归档时间: |
|
| 查看次数: |
625 次 |
| 最近记录: |