c++ - 错误 : expected type-specifier before ‘Elem’

标签 c++ templates inheritance

我正在尝试使用模板继承来实现数据结构,当我尝试插入一个元素时,出现以下错误:

pilha.h:15:21: error: expected type-specifier before ‘Elem’

this->topo = new Elem(dado);

以及与之相关的其他几个错误。

代码:

基础.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

基础.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"

主要.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);

关于c++ - 错误 : expected type-specifier before ‘Elem’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43551286/

相关文章:

python - Sklearn Pipeline - 如何在自定义 Transformer(不是 Estimator)中继承 get_params

c++ - is_trivially_destructible : invalid use of incomplete type ‘class Bar2’

c++ - g++ 无法找到此方法,即使它包含在内

python - 在 C++ 中获取 Python 函数参数名称

c++ - C 中的泛型指针和 C++ 中的泛型指针有哪些区别?

c++ - vector 类型的模板类特化 - 不同的有效语法?

c++ - 类模板的非模板函数友元

c++ - 仅当从 C++11 中的 B 派生时,如何有条件地调用 B::f?

java - 发现它指向什么继承类

java - 通用 <T extends A> 类使用静态字段而不是 T 的静态字段