错误:没有匹配函数调用‘std::__cxx11::basic_string<char>::basic_string(int&)’

Posted

技术标签:

【中文标题】错误:没有匹配函数调用‘std::__cxx11::basic_string<char>::basic_string(int&)’【英文标题】:error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(int&)’ 【发布时间】:2020-07-14 16:18:59 【问题描述】:

嗯,我有这个代码:

#include <iostream>
#include "Stack.h"
#include <string>

using namespace std;

int main(int argc, char* argv[])
    Stack<string> p(100);

    p.push("python");
    p.push("haskell");
    p.push("C++");

    //p.desempilhar();
    if(p.isEmpty())
        cout << "Pilha vazia!\n";
    else
        cout << "Pilha NAO vazia!\n";
    if(!p.isEmpty())
        cout << "Topo: " << p.peek() << endl;
    else
        cout << "A pilha esta vazia!!\n";
    return 0;

以及文件夹/home/matheus/Codes/C++/EstruturaDeDados中的这个.h代码:

#ifndef __STACK_H_
#define __STACK_H_

#include <iostream>

using namespace std;

/*
    Declarando a criação de um template para classe Stack.
    Stack aqui é um template, não uma classe propriamente dita.
    Ao ser declarada da maneira correta se torna uma classe de fato.
*/
template <class T>
class Stack 
    private:
        int top;
        T* a;
        int MAX;

    public:
        Stack(int MAX);
        bool push(T x); //Adiciona um T a stack.
        bool pop(); //Remove o T mais acima da stack.
        T peek(); //Retorna o T mais acima da stack.
        bool isEmpty(); 
;

//Declarando uso de um template. template <class T>
template <class T>
//"Stack<T>" é uma classe baseada no "template <class T>".
Stack<T>::Stack(int MAX)
    a = new T(MAX);
    top = -1;
    this->MAX = MAX;


//Declarando uso de um template. template <class T>
template <class T>
//"Stack<T>" é uma classe baseada no "template <class T>".
bool Stack<T>::push(T x) 
    if (top >= (MAX - 1))  
        cout << "Stack Overflow" << endl;
        return false; 
     else  
        a[++top] = x; 
        cout << x << " pushed into stack" << endl; 
        return true; 
     
 

//Declarando uso de um template.
template <class T>
//"Stack<T>" é uma classe baseada no "template <class T>".
bool Stack<T>::pop() 
    if (top < 0)  
        cout << "Stack Underflow" << endl; 
        return false; 
     
    else 
        cout << a[top--] << " Popped from stack" << endl;
        return true;
     
 

//Declarando uso de um template.
template <class T>
//"Stack<T>" é uma classe baseada no "template <class T>".
T Stack<T>::peek()  
    if (top < 0)  
        cout << "Stack is Empty" << endl;
        return NULL; 
     else  
        return a[top];
     
 

//Declarando uso de um template.
template <class T>
//"Stack<T>" é uma <<classe baseada no "template <class T>".
bool Stack<T>::isEmpty() 
    return (top < 0);


#endif

当我尝试编译时,我收到这个错误,我什至无法理解它是什么:

In file included from 21Templates.cpp:2:
/home/matheus/Codes/C++/EstruturaDeDados/Stack.h: In instantiation of ‘Stack<T>::Stack(int) [with T = std::__cxx11::basic_string<char>]’:
21Templates.cpp:8:21:   required from here
/home/matheus/Codes/C++/EstruturaDeDados/Stack.h:32:9: error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(int&)’
     a = new T(MAX);
         ^~~~~~~~~~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 21Templates.cpp:1:
/usr/include/c++/8/bits/basic_string.h:614: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,
         ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:614:9: note:   template argument deduction/substitution failed:
In file included from 21Templates.cpp:2:
/home/matheus/Codes/C++/EstruturaDeDados/Stack.h:32:9: note:   candidate expects 3 arguments, 1 provided
     a = new T(MAX);
         ^~~~~~~~~~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 21Templates.cpp:1:
/usr/include/c++/8/bits/basic_string.h:576:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
       basic_string(basic_string&& __str, const _Alloc& __a)
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:576:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/8/bits/basic_string.h:572:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
       basic_string(const basic_string& __str, const _Alloc& __a)
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:572:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/8/bits/basic_string.h:568:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::initializer_list<_Tp>, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
       basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:568:7: note:   no known conversion for argument 1 from ‘int’ to ‘std::initializer_list<char’
/usr/include/c++/8/bits/basic_string.h:541:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
       basic_string(basic_string&& __str) noexcept
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:541:7: note:   no known conversion for argument 1 from ‘int’ to ‘std::__cxx11::basic_string<char>&&’
/usr/include/c++/8/bits/basic_string.h:529:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
       basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:529:7: note:   candidate expects 3 arguments, 1 provided
/usr/include/c++/8/bits/basic_string.h:514:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ <near match>
       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:514:7: note:   conversion of argument 1 would be ill-formed:
In file included from 21Templates.cpp:2:
/home/matheus/Codes/C++/EstruturaDeDados/Stack.h:32:9: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
     a = new T(MAX);
         ^~~~~~~~~~
In file included from /usr/include/c++/8/string:52,
                 from /usr/include/c++/8/bits/locale_classes.h:40,
                 from /usr/include/c++/8/bits/ios_base.h:41,
                 from /usr/include/c++/8/ios:42,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from 21Templates.cpp:1:
/usr/include/c++/8/bits/basic_string.h:499:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int’
       basic_string(const _CharT* __s, size_type __n,
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:499:7: note:   candidate expects 3 arguments, 1 provided
/usr/include/c++/8/bits/basic_string.h:481:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
       basic_string(const basic_string& __str, size_type __pos,
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:481:7: note:   candidate expects 4 arguments, 1 provided
/usr/include/c++/8/bits/basic_string.h:465:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
       basic_string(const basic_string& __str, size_type __pos,
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:465:7: note:   candidate expects 3 arguments, 1 provided
/usr/include/c++/8/bits/basic_string.h:450:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
       basic_string(const basic_string& __str, size_type __pos,
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:450:7: note:   candidate expects 3 arguments, 1 provided
/usr/include/c++/8/bits/basic_string.h:437:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
       basic_string(const basic_string& __str)
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:437:7: note:   no known conversion for argument 1 from ‘int’ to ‘const std::__cxx11::basic_string<char>&’
/usr/include/c++/8/bits/basic_string.h:429:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
       basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:429:7: note:   no known conversion for argument 1 from ‘int’ to ‘const std::allocator<char>’
/usr/include/c++/8/bits/basic_string.h:420:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
       basic_string()
       ^~~~~~~~~~~~
/usr/include/c++/8/bits/basic_string.h:420:7: note:   candidate expects 0 arguments, 1 provided

.h 是堆栈的模板,我试图用以下代码编写所有这些:g++ -I /home/matheus/Codes/C++/EstruturaDeDados/ -o 21Templates 21Templates.cpp,但我一遍又一遍地接收这个错误。

我如何解决这个问题?那究竟是什么错误?

【问题讨论】:

好像你在 Stack 的构造函数中使用参数 MAX 调用 T 的构造函数。您应该使用a = new T[MAX];(方括号,而不是括号) 是的,就是这样!!! 【参考方案1】:

此声明

a = new T(MAX);

尝试从整数值 MAX 创建 std::string 类型的对象。但是 std::string 类没有这样的构造函数。

看来你的意思

a = new T[MAX];

也就是说,您要创建一个 std::string 类型的对象数组。

这个函数

T Stack<T>::peek()  
    if (top < 0)  
        cout << "Stack is Empty" << endl;
        return NULL; 
     else  
        return a[top];
     
 

也是错误的,因为从空指针创建 std::string 类型的对象会导致未定义的行为。您应该抛出异常,例如 std::out_of_range

注意这个类没有析构函数。

您可以使用std::vector&lt;std::string&gt; 类来代替动态分配的数组。

【讨论】:

谢谢,您解释了错误和解决方法。我没有看到我使用的是 ( ) 而不是 [ ]。 我还没有研究 C++ 的异常,我在 java 上知道,我记得在这些情况下我返回 null,而我还不知道 java 异常。【参考方案2】:

如果你想创建一个数组,改变这一行

a = new T(MAX);

到这里

a = new T[MAX];

记得稍后删除它,否则你会泄露内存

Stack::~Stack()

    delete[] a;

【讨论】:

如果你提到析构函数,你也应该提到三的规则。内存泄漏不会导致 UB,缺少复制构造函数可能会。 @Yksisarvinen 什么是三规则? @FUNKYBAT 看到这个问题:What is The Rule of Three?

以上是关于错误:没有匹配函数调用‘std::__cxx11::basic_string<char>::basic_string(int&)’的主要内容,如果未能解决你的问题,请参考以下文章

deque.at 无匹配函数

错误:没有匹配函数调用‘std::vector<std:

没有运算符“=”匹配这些操作数错误

如何在GDB中调用C ++函数?

将 std::__cxx11::string 转换为 std::string

将 std::__cxx11::string 转换为 std::string