gpt4 book ai didi

c++ - 无法将变量 'test' 声明为抽象类型 'OurStack'

转载 作者:行者123 更新时间:2023-11-28 01:49:55 24 4
gpt4 key购买 nike

我一直在寻找类似的答案,但他们都有参数错误或拼写错误。

我正在尝试创建一个 int 类型的堆栈,但我在使用 push 函数时遇到了问题。

准确的错误是:

 OurStack<int> test;
^
In file included from StackTester.cpp:5:0:
OurStack.h:7:7: note: because the following virtual functions are pure within 'OurStack<int>':
class OurStack : public StackInterface<T>
^
In file included from OurStack.h:4:0,
from StackTester.cpp:5:
StackInterface.h:11:15: note: bool StackInterface<T>::push(const T&) [with T = int]
virtual bool push(const T& newEntry) = 0;

这是我的代码:

堆栈接口(interface).h

/** @file StackInterface.h */ 
#ifndef _STACK_INTERFACE
#define _STACK_INTERFACE
template<class T> class StackInterface
{ public:

/** Sees whether this stack is empty. @return True if the stack is empty, or false if not. */
virtual bool isEmpty() const = 0;

/** Adds a new entry to the top of this stack. @post If the operation was successful, newEntry is at the top of the stack. @param newEntry The object to be added as a new entry. @return True if the addition is successful or false if not. */
virtual bool push(const T& newEntry) = 0;

/** Removes the top of this stack. @post If the operation was successful, the top of the stack has been removed. @return True if the removal is successful or false if not. */
virtual bool pop() = 0;

/** Returns the top of this stack. @pre The stack is not empty. @post The top of the stack has been returned, and the stack is unchanged. @return The top of the stack. */
virtual T peek() const = 0;
};

// end StackInterface
#endif

OurStack.h

#ifndef _STACK_H_
#define _STACK_H_

#include "StackInterface.h"

template <class T>
class OurStack : public StackInterface<T>
{
private:
std::stack<T> ourStack;

public:
OurStack();
bool isEmpty() const;
bool push(const T& newEntry) const;
bool pop();
T peek() const;

};
#include "OurStack.cpp"
#endif

OurStack.cpp

#include "OurStack.h"

template <class T>
OurStack<T>::OurStack()
{
//not much to do between these
}

template <class T>
bool OurStack<T>::isEmpty() const
{
return ourStack.empty();
}

template <class T>
bool OurStack<T>::push(const T& newEntry) const
{
ourStack.push(&newEntry);
return true;
}

template <class T>
bool OurStack<T>::pop()
{
if(ourStack.empty() == true)
return false;
else
{
ourStack.pop();
return true;
}
}

template <class T>
T OurStack<T>::peek() const
{
return ourStack.top();
}

堆栈测试器.cpp

#include <iostream>
#include <cstdlib>
#include <string>
#include <stack>
#include "OurStack.h"

using namespace std;

OurStack<int> test;
int any;

int main()
{
cout<<"press any number";
cin>>any;
return 0;
}

最佳答案

你声明bool OurStack<T>::push(const T& newEntry) const;但是你的基类声明了 bool push(const T& newEntry); . push不是 const .使用 override 避免这些问题的关键字。

template <class T>
class OurStack : public StackInterface<T>
{
private:
std::stack<T> ourStack;

public:
OurStack();
bool isEmpty() const override; // Compiles
bool push(const T& newEntry) const override; // Doesn't compile
bool pop() override; // Compiles
T peek() const override; // Compiles

};

关于c++ - 无法将变量 'test' 声明为抽象类型 'OurStack<int>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43419114/

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