学习c++,编译报错“no matching function for call”

Blu*_*its 0 c++

在 Accelerated C+ 一书中,我正在尝试运行代码

#include <iostream>
#include <string>

int main() {
std::cout << "Please enter  your name: ";
std::string name;
std::cin >> name;

const std::string greeting = "Hello, " + name + "!";


const std::string spaces(greeting.size(), " ");
const std::string second = "* " + spaces + " *";

const std::string first(second.size(), + "*");

std::cout << std::endl;
std::cout << first << std::endl;
std::cout << second << std::endl;
std::cout << "* " << greeting << " *" << std::endl;
std::cout << second << std::endl;
std::cout << first << std::endl;

return 0;
}
Run Code Online (Sandbox Code Playgroud)

它给了我一个编译错误,上面写着:

c:\Users\darkL\Desktop\cLearn\learnOne.cpp: In function 'int main()':
c:\Users\darkL\Desktop\cLearn\learnOne.cpp:15:47: error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(std::__cxx11::basic_string<char>::size_type, const char [2])'
     const std::string first(second.size(), "*");
                                               ^
In file included from C:/Mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/string:52,
                 from C:/Mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/locale_classes.h:40,
                 from C:/Mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/ios_base.h:41,
                 from C:/Mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/ios:42,
                 from C:/Mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/ostream:38,
                 from C:/Mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/iostream:39,
                 from c:\Users\darkL\Desktop\cLearn\learnOne.cpp:1:
C:/Mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/basic_string.h:604:9: note: candidate: 'template<class _InputIterator, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&)'
         basic_string(_InputIterator __beg, _InputIterator __end,
         ^~~~~~~~~~~~
C:/Mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/basic_string.h:604:9: note:   template argument deduction/substitution failed:
c:\Users\darkL\Desktop\cLearn\learnOne.cpp:15:47: note:   deduced conflicting types for parameter '_InputIterator' ('unsigned int' and 'const char*')
     const std::string first(second.size(), "*");
                                                  ^
Run Code Online (Sandbox Code Playgroud)

这是否意味着 size() 函数对问候无效?还是空格()无效?

cig*_*ien 5

std::string 构造函数的第二个参数需要是 a char,而不是 a string

const std::string spaces(greeting.size(), ' ');
Run Code Online (Sandbox Code Playgroud)

您还需要对以下行执行相同的操作,并去掉+符号:

const std::string first(second.size(), + "*");  // wrong
Run Code Online (Sandbox Code Playgroud)

像这样:

const std::string first(second.size(), '*');
Run Code Online (Sandbox Code Playgroud)

请注意,您可以+留在那里,但它不会做任何有用的事情,并且会使阅读您的代码的人感到困惑,因此您应该避免它。