gpt4 book ai didi

c++ - 错误 : no matching function for call to ‘std::__cxx11::basic_string::basic_string(int&)’

转载 作者:行者123 更新时间:2023-12-01 14:48:18 26 4
gpt4 key购买 nike

好吧,我有这个代码:

#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,但我一遍又一遍地接收这个错误。

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

最佳答案

这个声明

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<std::string> 类代替动态分配的数组.

关于c++ - 错误 : no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(int&)’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60995908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com