Home > OS >  What's the connection of string_view and basic_string<char> and why does string_view exam
What's the connection of string_view and basic_string<char> and why does string_view exam

Time:02-10

I have copied code from Bjarne Stroustrup's A Tour of C to test out string views but I keep getting error:

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

I am using VS Code WSL 2 with Ubuntu 20.04 and gcc-11

in main.cpp, I have:

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

using namespace std;

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

in strings.h, I have the following. The function is copied as in the text book. I typed it out to avoid the peculiar * character that had a different encoding.

#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;
}

Using the Ctrl F5 command gives this error.

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:

I took a screenshot of what I see from Visual Studio code to indicate the underlined code, which doesn't makes

Image view of the error in VS Code

I thought it might have been the code editor but Godbolt shows similar errors.

Since this is related to Vs Code, here are my

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"
        }
    ]
}

and 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
}

CodePudding user response:

Yes, this seems to be wrong.

The line

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

should be

string res(sv1.length() sv2.length(), '\0');

std::string doesn't have a constructor which takes only a size argument, in contrast to std::vector. The value with which the elements are to be initialized must be provided explicitly.


I couldn't find this mistake in the list of errata here, though.

  • Related