C++에서 템플릿 상속할때...
#include
using namespace std;
template
class Bag{
T *array;
int capacity;
int top;
public:
Bag(int bagCapacity){
if(bagCapacity < 0) throw "Bag capacity must be >0";
capacity = bagCapacity;
array = new T[bagCapacity];
top = -1;
}
virtual ~Bag(){
delete[] array;
}
virtual int Size(){
return top+1;
}
virtual bool IsEmpty(){
return (top == -1);
}
virtual T& element(){
if(IsEmpty()) throw "Bag is empty";
return array[0];
}
virtual void Push(T x){
if(top+1 == capacity){
T *newBag = new T[2*capacity];
for(int i = 0; i < capacity; i++){
newBag[i] = array[i];
}
capacity *= 2;
delete[] array;
array = newBag;
}
top += 1;
array[top] = x;
}
virtual void Pop();
};
template
class Stack: public Bag{
public:
inline Stack(int stackCapacity){
if(stackCapacity <0) throw "Stack capacity must be >0";
this.capacity = stackCapacity;
this.array = new T[stackCapacity];
this.top = -1;
}
~Stack(){
delete[] this.array;
}
T& Top()const{
if(this.IsEmpty()) throw "Stack is empty.";
return this.array[this.top];
}
void Pop(){
if(this.IsEmpty()) throw "Stack is empty.";
this.array[this.top] = '\0';
this.top -= 1;
}
};
int main(){
Stack test(10);
return 0;
}
라고 했는데 저기 Stack 에서 constructor가 잘못됐다는 거는 알겠는데 어떻게 고쳐야 하는지 모르겠습니다..
템플릿 관련 상속은 전혀 감이 잡히지 않아서..
템플릿이 아니라도
템플릿이 아니라도 문제가 생기는 상황입니다.
Bag에 default ctor이 없으니 명시적으로 불러줘야죠.
[예진아씨 피카사 웹앨범] 임예진 팬클럽 ♡예진아씨♡ http://cafe.daum.net/imyejin
못알아듣겠어요 ㅠ
생초보라.. 어떻게 해야하는거죠 ㅠㅠ
Stack(int stackCapacity)
댓글 달기