2016-12-22 3 views
1

Создал абстрактный класс, который называется «список» и унаследовал «список» для создания очереди и стека. Но когда я попытался скомпилировать код, у меня появились ошибки, указывающие, что «tail, head, next» не распознаются или неизвестны.Наследование класса шаблона дает неизвестную переменную error

#include <iostream> 
#include <cstdlib> 

using namespace std; 

template <typename T> 
class list{ 
public: 
    T data; 
    list<T> * head; 
    list<T> * tail; 
    list<T> * next; 
    list(){head = tail = next = NULL;} 
    virtual ~list(){} 
    virtual void store(T) = 0; 
    virtual T retrieve() = 0; 
}; 

// QUEUE 
template <typename T> 
class queue : public list<T>{ 
public: 
    virtual void store(T); 
    virtual T retrieve(); 
}; 

template <typename T> 
void queue<T>::store(T d){ 
    list<T> * temp = new queue<T>; 
    if (!temp) exit(EXIT_FAILURE); 

    temp->data = d; 
    temp->next = NULL; 

    if(tail){ 
     tail->next = temp; 
     tail = temp; 
    } 

    if(!head) head = tail = temp; 

} 

template <typename T> 
T queue<T>::retrieve(){ 
    T i; 
    list<T> * temp; 

    i = head->data; 
    temp = head; 
    head = head->next; 

    delete temp; 

    return i; 
} 



// STACK 
template <typename T> 
class stack : public list<T>{ 
public: 
    virtual void store(T i); 
    virtual T retrieve(); 
}; 

template <typename T> 
void stack<T>::store(T d){ 
    list<T> * temp = new stack<T>; 
    if(!temp) exit(EXIT_FAILURE); 

    temp->data = d; 

    if (tail) temp->next = tail; 
    tail = temp; 
    if(!head) head = tail; 

} 

template <typename T> 
T stack<T>::retrieve(){ 
    T i; 
    list<T> * temp; 

    i = tail->data; 
    temp = tail; 
    tail = tail->next; 

    delete temp; 

    return i; 
} 




int main(){ 

    queue<int> mylist; 

    for(int i = 0; i < 10; i++) 
     mylist.store(i); 

    for(int i = 0; i < 10; i++) 
      cout << mylist.retrieve() << endl; 
} 

я создал абстрактный класс, который называется «список», и я унаследовал «список» класс, чтобы создать очередь и стек. Но когда я попытался скомпилировать код, у меня появились ошибки, указывающие, что «tail, head, next» не распознаются или неизвестны.

ошибка ниже:

..\main.cpp: In member function 'virtual T stack<T>::retrieve()': 
..\main.cpp:86:6: error: 'tail' was not declared in this scope 
+0

Не описывайте сообщения об ошибках, вставьте их. –

ответ

2

Явный относится к области базового класса, чтобы получить доступ к наследуемым переменным-членам:

if(list<T>::tail){ 
// ^^^^^^^^^ 
    list<T>::tail->next = temp; 
// ^^^^^^^^^ 
    list<T>::tail = temp; 
// ^^^^^^^^^ 
} 

Доступ через this может быть использован в качестве альтернативы:

if(this->tail){ 
// ^^^^^^ 
    this->tail->next = temp; 
// ^^^^^^ 
    this->tail = temp; 
// ^^^^^^ 
} 
+0

Можете ли вы объяснить мне, почему мы должны явно писать переменные? мы уже определили его, поскольку мы унаследовали базовый класс. –

+0

@ RıfatTolgaKiran Они не были объявлены в текущем объеме, как сказал компилятор. Вы можете использовать 'this->' альтернативно. –

+1

Хм спасибо за ваши ответы. Они помогли мне понять многое. –

Смежные вопросы