2017-04-14 58 views
0

我一直在尋找類似的答案,他們都有參數錯誤或錯別字。不能聲明變量'測試'是抽象類型'OurStack <int>'

我想創建一個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; 

這裏是我的代碼:

StackInterface.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

​​

StackTester.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; 
} 
+0

您使用的是Visual Studio嗎? – Omore

+0

nope,notepad ++ –

回答

2

您聲明bool OurStack<T>::push(const T& newEntry) const;但你的基類聲明bool push(const T& newEntry);push不是const。使用override關鍵字來避免這些問題。

+0

謝謝,它在提供的StackInterface中是const,它似乎很奇怪,一定是錯誤的 編輯:不,它不是,我犯了錯誤。可能更好 –

0

怎麼能推功能:

bool push(const T& newEntry) const; 

可能是const?

而且,用戶名是這樣的:

_STACK_H_ 

被保留用於執行。只需使用:

STACK_H