ошибка: нет соответствующей функции для вызова 'std :: __ cxx11 :: basic_string <char>:: basic_string (int &)' - PullRequest
2 голосов
/ 02 апреля 2020

Ну, у меня есть этот код:

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

и этот код .h в папке / home / matheus / Codes / C ++ / EstruturaDeDados:

#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 / Коды / C ++ / EstruturaDeDados / -o 21 Шаблоны 21 Шаблоны. cpp, но я получаю эту ошибку снова и снова.

Как мне решить эту проблему? И что именно эта ошибка?

Ответы [ 2 ]

1 голос
/ 02 апреля 2020

Этот оператор

a = new T(MAX);

пытается создать объект типа std :: string из целочисленного значения MAX. Однако класс 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<std::string>.

1 голос
/ 02 апреля 2020

Если вы пытаетесь создать массив, измените эту строку

a = new T(MAX);

на эту

a = new T[MAX];

и не забудьте удалить ее позже, иначе вы потеряете эту память

Stack::~Stack()
{
    delete[] a;
}
...