C++ 错误:在‘;之前应为类型说明符;元素’;
我尝试使用模板继承来实现数据结构,当我尝试插入元素时,会出现以下错误: pilha.h:15:21:错误:“Elem”之前应为类型说明符 此->地形=新要素(护墙板) 以及其他一些与之相关的错误 守则: base.hC++ 错误:在‘;之前应为类型说明符;元素’;,c++,templates,inheritance,C++,Templates,Inheritance,我尝试使用模板继承来实现数据结构,当我尝试插入元素时,会出现以下错误: pilha.h:15:21:错误:“Elem”之前应为类型说明符 此->地形=新要素(护墙板) 以及其他一些与之相关的错误 守则: base.h #ifndef __BASE_H_INCLUDED__ #define __BASE_H_INCLUDED__ #include <string> #include <iostream> using namespace std; template <
#ifndef __BASE_H_INCLUDED__
#define __BASE_H_INCLUDED__
#include <string>
#include <iostream>
using namespace std;
template <class T>
class Base {
protected:
class Elem;
public:
Base(){
tam = 0;
}
virtual ~Base(){
if (tam == 0) return;
for(Elem *aux = topo; aux != NULL; aux = aux->ant) {
Elem *aux2 = aux;
delete(aux2);
}
}
bool vazia(){
return tam == 0;
}
unsigned int tamanho(){
return tam;
}
virtual T retira() = 0;
virtual void imprime(){
if(tam == 0) {
cout << "A " << nome_da_classe << " esta vazia!" << endl;
return;
}
cout << "Impressao = ";
for(Elem *aux = topo; aux != 0; aux = aux->ant) {
cout << aux->dado << " ";
}
cout << endl;
}
T ver_topo(){
if (tam == 0) {
cout << "A " << nome_da_classe << " esta vazia!" << endl;
return 0;
}
return topo->dado;
}
protected:
int tam;
class Elem{
public:
T dado;
Elem *prox;
Elem *ant;
Elem(T d){
dado = d;
}
};
Elem *topo;
Elem *base;
string nome_da_classe;
};
#endif
皮尔哈
#ifndef __PILHA_H_INCLUDED__
#define __PILHA_H_INCLUDED__
#include "base.h"
template <class T>
class Pilha : public Base<T> {
public:
Pilha(){
this->topo = nullptr;
this->nome_da_classe = "Pilha";
}
~Pilha(){}
void insere(T dado){
if (!this->tam) {
this->topo = new Elem(dado);
this->topo->ant = nullptr;
} else {
Elem *elem = new Elem(dado);
elem->ant = this->topo;
this->topo = elem;
}
this->tam++;
}
T retira(){
if (this->tam == 0) {
cout << "A "<< this->nome_da_classe << " esta vazia!" << endl;
return 0;
}
T aux = this->topo->dado;
Elem *aux2 = this->topo;
this->topo = this->topo->ant;
delete(aux2);
this->tam--;
return aux;
}
};
#endif
#如果包括__
#定义包含的内容
#包括“base.h”
模板
Pilha类:公共基础{
公众:
皮尔哈(){
此->拓扑=空PTR;
这->nome_da_classe=“Pilha”;
}
~Pilha(){}
空心镶嵌(T护墙板){
如果(!this->tam){
此->地形=新要素(护墙板);
此->拓扑->ant=nullptr;
}否则{
元素*元素=新元素(护墙板);
元素->蚂蚁=这个->地形;
本->地形=要素;
}
这个->tam++;
}
T retira(){
如果(此->tam==0){
cout-topo;
这个->地形=这个->地形->蚂蚁;
删除(aux2);
这个->谭--;
返回aux;
}
};
#恩迪夫
pilha.cpp
#include <iostream>
#include "base.h"
#include "pilha.h"
#包括
#包括“base.h”
#包括“pilha.h”
main.cpp
#include <iostream>
#include "pilha.h"
using namespace std;
int main() {
Pilha<int> *b = new Pilha<int>();
b->imprime();
b->insere(5);
delete(b);
return 0;
}
#包括
#包括“pilha.h”
使用名称空间std;
int main(){
Pilha*b=新Pilha();
b->impime();
b->插图(5);
删除(b);
返回0;
}
任何意见都会非常有用。依赖基类(其类型取决于一个或多个模板参数的基类)的成员无法通过普通名称查找找到。您需要拼写出
typename base::Elem
:
this->topo = new typename Base<T>::Elem(dado);
this->topo=newtypename Base::Elem(dado);
普通名称查找找不到依赖基类(其类型取决于一个或多个模板参数的基类)的成员。您需要拼出typename base::Elem
:
this->topo = new typename Base<T>::Elem(dado);
this->topo=newtypename Base::Elem(dado);
没有叫做Elem
的类,这是编译器告诉您的。有一个内部类Elem
,它是模板类的成员,但这与Elem
类无关。一个完全不相关的模板正在尝试引用。哦,您也应该这样做。因为有一个内部c模板类中的LASS并不意味着您可以直接从另一个类引用它。C++不这样工作。请注意,您在<代码> ~(基)中进行安全销毁的尝试。一点也不安全,因为aux
仍然指向被删除的对象。@SamVarshavchik你说的好像问题在于它是一个内部类。但是如果我们继承,比如说Base
而不是Base
,这将是完全合法的。你到目前为止做了什么?我想Elem
不被认为是类型。q将其标准化(Base::Elem
)或者声明一个别名。没有叫做Elem
的类,这是编译器告诉您的。有一个内部类Elem
,它是模板类的成员,但这与Elem
类无关。一个完全不相关的模板正在尝试引用。哦,您也应该这样做。因为E是模板类中的一个内部类,并不意味着你可以直接从另一个类引用它。C++不这样工作。注意你在代码< >()中的安全破坏尝试。一点也不安全,因为aux
仍然指向被删除的对象。@SamVarshavchik你说的好像问题在于它是一个内部类。但是如果我们继承,比如说Base
而不是Base
,这将是完全合法的。你到目前为止做了什么?我想Elem
不被认为是类型。q将其命名(Base::Elem
)或声明一个别名。因此,我使用typename向编译器解释“Base::Elem”是一种类型?是的。另请参阅,因此我使用typename向编译器解释“Base::Elem”是一种类型?是的。请参阅