【发布时间】:2014-04-05 11:53:24
【问题描述】:
我有一个文件my_node.h。在这个文件中,我声明了一个 Stack 类:
template<class Datatype>
class Stack
{
public:
Stack() : head( NULL )
{
}
virtual ~Stack()
{
Datatype temp;
while (pop(temp));
cout << "Stack released all spaces" << endl;
}
public:
virtual int push(Datatype &);
virtual int pop(Datatype &);
virtual Datatype* peek();
protected:
Node<Datatype> *head;
};
然后,我有另一个名为 new_stack.h 的文件。在这个文件中,我写了一个Stack的继承类,即StackWithDeep。代码如下:
#include "my_node.h"
#include <iostream>
#include <list>
using namespace std;
template<class Datatype>
class StackWithDeep : public Stack<Datatype>
{
public:
StackWithDeep(int thre) : Stack<Datatype>()
{
stack_deep = 0;
limited_deep = thre;
}
virtual ~StackWithDeep()
{
}
public:
virtual int push(Datatype &);
virtual int pop(Datatype &);
int getdeep()
{
return stack_deep;
}
private:
int stack_deep;
int limited_deep;
};
template<class Datatype>
int StackWithDeep<Datatype>::push(Datatype &new_data)
{
Node<Datatype> *pt_node = new Node<Datatype>;
if (pt_node == NULL)
return -1;
if (stack_deep != limited_deep)
{
pt_node -> addData(new_data);
if (head == NULL)
head = pt_node;
else
{
pt_node -> addPrev(*head);
head = pt_node;
}
stack_deep ++;
return 1;
}
else
{
delete pt_node;
return 0;
}
return 0;
}
我想实现一个 push()。但是,当我编译时,编译器说:
In member function ‘virtual int StackWithDeep<Datatype>::push(Datatype&)’:
error: ‘head’ was not declared in this scope
我想我可以使用这个 head 指针,因为它在类 Stack 中受到保护,并且我的新类公开继承。
【问题讨论】:
标签: c++ inheritance scope