2017-04-14 1 views
0

비슷한 답변을 보았는데 모두 매개 변수가 잘못되었거나 오타가있었습니다.'test'변수를 추상 형식으로 선언 할 수 없습니다. '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

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

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

. pushconst이 아닙니다. 이러한 문제를 방지하려면 override 키워드를 사용하십시오.

+0

감사합니다. 제공된 StackInterface에서 const 였고 이상하게 보였습니다. 실수 였음에 틀림 없습니다. 편집 : 아니요, 실수였습니다. 아마 더 좋다. –

0

어떻게 푸시 기능을 수행 할 수 있습니다 :

bool push(const T& newEntry) const; 

이 가능하게 될 const를? 이 같은 또한

, 이름 :

_STACK_H_ 

은 구현을 위해 예약되어 있습니다. 간단하게 사용 : 당신은 bool OurStack<T>::push(const T& newEntry) const;를 선언하지만 기본 클래스 bool push(const T& newEntry);를 선언

STACK_H 
관련 문제