c++ - using vectors to make generic stacks -
so trying define stack class, uses vectors make generic stack. reason when try create new stack receive error saying nothing was declared, understand saying nothing made, never made new object. did follow guide on how make generic stacks. http://www.tutorialspoint.com/cplusplus/cpp_templates.htm
here stack class used:
#ifndef stackers #define stackers #include <vector> #include "wrapper.hh" template <class t> class stack { private: vector<t*> elems; public: stack(); void push(t* obj); // push element t* pop(); t* peek(); // pop element t* findmax(); t* findmin(); bool isempty(); }; template <class t> bool stack<t>:: isempty(){ return elems.size()==0; } template <class t> void stack<t>::push (t* obj) { // append copy of passed element elems.push_back(obj); } template <class t> t* stack<t>::peek () { // append copy of passed element elems.at(elems.size()); } template <class t> t* stack<t>::pop () { if (elems.empty()) { fatal("stack<>::pop(): empty stack"); } // remove last element return elems.pop_back(); } template<class t> t* stack<t>:: findmax(){ t* temp=new t*; for(int i=0;i<elems.size();i++){ if(elems.at(i)>temp) temp=elems.at(i); } return temp; } template<class t> t* stack<t>:: findmin(){ t* temp; for(int i=0;i<elems.size();i++){ if(elems.at(i)<temp) temp=elems.at(i); } return temp; } #endif
when call in main:
stack<pictures> stackers;
where picture object have made in class. throws error, "error: declaration not declare [-fpermissive]". assumed didn't need constructor due example following. not case non-primative data types?
after say
#define stackers
at top of header,
stack<pictures> stackers;
becomes
stack<pictures> ;
...which indeed not declare anything.
Comments
Post a Comment