gpt4 book ai didi

c++ - 如何修复 "use of class template requires template argument list"

转载 作者:行者123 更新时间:2023-11-27 23:39:46 27 4
gpt4 key购买 nike

我使用模板编写了一些关于堆栈的代码。但现在,坚持这个小错误。我尽可能地尝试了很多次,但不知道该怎么做。另外,我看到很多相同的错误问题,但我的问题似乎有所不同。

我正在写作中。所以,有一些未完成的代码。如果您看到错误但不是导致错误的原因,请忽略它。

#ifndef STACKINTERFACE_H
#define STACKINTERFACE_H

using namespace std;

template <typename T>
class StackInterface {
public:
StackInterface() {};
virtual ~StackInterface() {};
virtual bool isEmpty() const = 0;
virtual bool isFull() const = 0;
virtual void push(T data) = 0;
virtual void pop() = 0;
virtual T top() const = 0;
};


#include "StackInterface.h"

#include <iostream>
template <typename T>
class Stack : public StackInterface {
private:
T * items;
const static int max = 10;
const static int GROWBY = 5;
size_t arrayCapacity;
size_t numItems;
int top_position;
public:
Stack();
virtual void push(T data);
virtual T top() const;
virtual void pop();
virtual bool isEmpty() const;
virtual bool isFull() const;
size_t getArrayCapacity();
size_t getNumItems();
Stack<T> operator=(const Stack<T>&Other);
~Stack();
Stack(const Stack<T>& other);
};

template <typename T>
Stack<T>::Stack()
{
items = new T[max];
numItems = 0;
top_position = -1;
}

template <typename T>
Stack<T>::~Stack() {
cout << "deleting..." << endl;
delete[] items;
}

template <typename T>
void Stack<T>::push(T data) {
cout << "inserting" << data << endl;
if (numItems < arrayCapacity) {
T[++top_position] = data;
}
numItems++;
}
template <typename T>
void Stack<T>::pop() {
if (isEmpty) {
cout << "The stack is empty. Returned value not reliable.\n";
}
else {
cout << "removing" << top() << endl;
retrun T[top_position--];
}
}

template <typename T>
bool Stack<T>::isEmpty() const
{
if (numItems == 0) {
return true;
}
else {
return false;
}
}

template <typename T>
bool Stack<T>::isFull() const
{
if (numItems == max) {
return true;
}
else {
return false;
}
}

template <typename T>
T Stack<T>::top() const
{
if (!isEmpty()) {
return T[top_position];
}
else {
throw exception;
}
}

template <typename T>
Stack <T> Stack<T>:: operator=(const Stack<T>&Other) {
if (&Other == this) {
return *this;
}
}

template <typename T>
Stack<T>::Stack(const Stack<T>& other) {
items = new items(other.items);
numItems = other.numItems;
top_position = other.top_position;
arrayCapacity = other.arrayCapacity;
}

template <typename T>
size_t Stack<T>::getArrayCapacity() {
return arrayCapacity;
}

template <typename T>
size_t Stack<T>::getNumItems() {
return numItems;
}

int main() {

Stack <int> S1;


system("pause");
return 0;
}

最佳答案

您需要为您的父类(super class)提供模板参数。而不是这个:

template <typename T>
class Stack : public StackInterface {

这样做:

template <typename T>
class Stack : public StackInterface<T> {

(您的代码也有很多其他问题,但这就是您解决所询问的特定问题的方式。)

关于c++ - 如何修复 "use of class template requires template argument list",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56301352/

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