I create a class Stack, which inherits LinkedList, but gives an error when compiling, tell me what the problem is.
Mistake:
"D: \ CLion 2016.3.3 \ bin \ cmake \ bin \ cmake.exe" - build D: \ workspaceForCLion \ StudyProj \ cmake-build-debug --target StudyProj - -j 2 Scanning dependencies of target StudyProj [50 %] Building CXX object CMakeFiles / StudyProj.dir / main.cpp.obj In the file included from D: \ workspaceForCLion \ StudyProj \ main.cpp: 2: 0: D: \ workspaceForCLion \ StudyProj \ Stack.h: 8: 35: error: expected class-name before '{' token class Stack: protected LinkedList {^ D: \ workspaceForCLion \ StudyProj \ Stack.h: Instantiation of 'void Stack :: push (T) [with T = int]': D : \ workspaceForCLion \ StudyProj \ main.cpp: 6: 17: required from here D: \ workspaceForCLion \ StudyProj \ Stack.h: 43: 11: error: 'class Stack' has no member named 'prepend' this-> prepend ( data); ~~~~~~~~~~~ D: \ workspaceForCLion \ StudyProj \ Stack.h: Instantiation of 'T Stack :: pop () [with T = int]': D: \ workspaceForCLion \ StudyProj \ main.cpp: 12: 23: required from here D: \ workspaceForCLion \ StudyProj \ Stack.h: 49: 11: error: 'class Stack' has no member named 'remove' this-> remove (res); ~~~~~~~~~~~ CMakeFiles \ StudyProj.dir \ build.make: 61: recipe for target 'CMakeFiles / StudyProj.dir / main.cpp.obj' failed mingw32-make.exe [3]: * [CMakeFiles / StudyProj.dir / main.cpp.obj] Error 1 mingw32-make.exe [2]: [CMakeFiles / StudyProj.dir / all] Error 2 CMakeFiles \ Makefile2: 66: recipe for target 'CMakeFiles / StudyProj. dir / all 'failed CMakeFiles \ Makefile2: 78: recipe for target' CMakeFiles / StudyProj.dir / rule 'failed Makefile: 117: recipe for target' StudyProj 'failed mingw32-make.exe [1]: [CMakeFiles / StudyProj.dir / rule] Error 2 mingw32-make.exe: * [StudyProj] Error 2
LinkedList.h
#pragma once #include <iostream> using namespace std; template<class T> class LinkedList { private: struct node { T data; node *next; } *head; public: LinkedList(); ~LinkedList(); void append(T data); void prepend(T data); void remove(T data); void clear(); void output(); }; template <class T> LinkedList<T>::LinkedList() { head=NULL; }; template <class T> LinkedList<T>::~LinkedList() { node *p, *q; p = head; if(p==NULL) return; while(p){ q = p->next; delete p; p = q; } } template <class T> void LinkedList<T>::prepend(T data){ node *p, *q; if(head==NULL){ head = new node; head->data = data; head->next = NULL; return; } p = this->head; q = new node; q->data = data; q->next = p; this->head = q; } template <class T> void LinkedList<T>::remove(T data){ node *p, *q; if(head == NULL) return; p = head; while(p){ if(p->data == data){ q->next = p->next; delete p; return; } q = p; p = p->next; } } template <class T> void LinkedList<T>::clear(){ node *p, *q; if(head==NULL) return; p = head; while(p){ q = p->next; delete p; if(q != head){ head = NULL; return; } p = q; } } template <class T> void LinkedList<T>::append(T data){ node *p, *q; if(head==NULL){ head = new node; head->data = data; head->next = NULL; return; } p = head; while(p->next!=NULL) p = p->next; q = new node; q->data = data; q->next = NULL; p->next = q; } template <class T> void LinkedList<T>::output() { if(this->head == NULL){ cout << "List is empty" << endl; return; } node *p; p = this->head; while (p != NULL) { cout << p->data << " -> "; p = p->next; } cout << "NULL" << endl; } Stack.h
#pragma once #include <iostream> #include "LinkedList.h" template<class T> class Stack : protected LinkedList{ private: struct node { T data; node *next; } *top; node *down; public: Stack(); ~Stack(); void push(T); T pop(); T peek(); void output(); }; template <class T> Stack<T>::Stack() { top = down = NULL; }; template <class T> Stack<T>::~Stack() { node *p, *q; p = top; if(p==NULL) return; while(p){ q = p->next; delete p; p = q; } }; template <class T> void Stack<T>::push(T data){ this->prepend(data); } template <class T> T Stack<T>::pop(){ T res = this->top->data; this->remove(res); return res; } template <class T> T Stack<T>::peek(){ return this->top->data; } template <class T> void Stack<T>::output(){ this->output(); } main.cpp
#include <conio.h> #include "Stack.h" int main(int argc, char** argv) { Stack<int> stack; stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.push(5); cout << stack.pop(); _getch(); return 0; }