错误:“ elem”之前的预期类型特定符
error: expected type-specifier before ‘Elem’
我正在尝试使用模板继承来实现数据结构,当我尝试插入元素时,它会产生以下错误:
pilha.h:15:21:错误:" elem"之前的预期类型特征
this-> topo = new elem(dado(;
以及与之相关的其他几个错误。
代码:
base.h
#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
base.cpp
#include "base.h"
pilha.h
#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
pilha.cpp
#include <iostream>
#include "base.h"
#include "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;
}
任何意见都将真正有帮助。
依赖基类的成员(类型取决于一个或多个模板参数的基类(未通过普通名称查找而找到。您需要拼写typename Base<T>::Elem
:
this->topo = new typename Base<T>::Elem(dado);
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 在数据类型类的列表中查找 elem
- 错误:“ elem”之前的预期类型特定符
- Elem没有命名类型
- 这种类型的模板参数是什么意思 - 'ClassA<T> T::*ELEM'?