Link to home
Start Free TrialLog in
Avatar of blaksaga
blaksaga

asked on

Templated stack

Sorry for all the code but I am trying to template a stack.  I do not understand what is going wrong...no matter what I do I get errors.  Any help on this code is appreciated.  Thanks


convert.cpp
-=-=-
//Includes
#include "stack.h"
#include <iostream>
using namespace std;

//Main function
int main()
{
     stack <char>equation;

     return 0;
}


stack.h
-=-=-
template <class T>
class stack
{
     public:
          stack();
          ~stack();
          bool empty() const;
          bool full() const;
          void push(const T);
          T pop();
          T peek() const;
     private:
          T *arr;
          int size;
          int top;
          stack(const stack &);
          const stack & operator = (const stack &);
          static const int init_size=5;
};


stack.cpp
-=-=-
#include <iostream>
#include "stack.h"
using namespace std;

template<class T>
stack<T>::stack()
{
     size=init_size;
     arr=new T[size];
     top=-1;
     return;
}

template<class T>
stack<T>::~stack()
{
     delete []arr;
     return;
}

template<class T>
bool stack<T>::empty() const
{
     return (top<0);
}

template<class T>
bool stack<T>::full() const
{
     return (top>=(size-1));
}

template<class T>
T stack<T>::peek() const
{
     T rval=0;
     if(empty())
     {
          cerr << "peek error" << endl;
     }else
     {
          rval=arr[top];
     }
     return rval;
}

template<class T>
T stack<T>::pop()
{
     T rval=peek();
     --top;
     return rval;
}

template<class T>
void stack<T>::push(const T foo)
{
     if(full())
     {
          int nsize=size+init_size;
          T *narr=new T[nsize];
          assert(narr);
          for(int i=0; i<size; ++i)
          {
               narr[i]=arr[i];
          }
          delete []arr;
          size=nsize;
          arr=narr;
     }
     arr[++top]=foo;
     return;
}
Avatar of mnashadka
mnashadka

There are a couple of things that could be problems here.  First and foremost, a templates definitions have to go into the header file.  There's typically no source (cpp) file.  The second thing is that there's already a class named "stack" in the library (so I assume that this is homework).  You'll either want to rename stack to something else (like Stack or MyStack), or make sure that there's no reference to the std::stack class.  Good luck!
Avatar of blaksaga

ASKER

template definitions in the header file?  can you give an example of what you mean?  yes it is homework and im new to this and am kinda confused!
template definitions in the header file?  can you give an example of what you mean?  yes it is homework and im new to this and am kinda confused!
So below is my stack.h.  But when I declare a stack called equation and do "har q=equation.peek;"it doesnt work.

stack.h
-=-=-
#include <iostream>
using namespace std;

template <class T>
class stack
{
     public:
          stack();
          ~stack();
          bool empty() const;
          bool full() const;
          void push(const T);
          T pop();
          T peek() const;
     private:
          T *arr;
          int size;
          int top;
          static const int init_size=5;
};

template<class T>
stack<T>::stack()
{
     size=init_size;
     arr=new T[size];
     top=-1;
     return;
}

template<class T>
stack<T>::~stack()
{
     delete []arr;
     return;
}

template<class T>
bool stack<T>::empty() const
{
     return (top<0);
}

template<class T>
bool stack<T>::full() const
{
     return (top>=(size-1));
}

template<class T>
T stack<T>::peek() const
{
     T rval;
     if(empty())
     {
          cerr << "peek error" << endl;
     }else
     {
          rval=arr[top];
     }
     return rval;
}

template<class T>
T stack<T>::pop()
{
     T rval=peek();
     --top;
     return rval;
}

template<class T>
void stack<T>::push(const T foo)
{
     if(full())
     {
          int nsize=size+init_size;
          T *narr=new T[nsize];
          assert(narr);
          for(int i=0; i<size; ++i)
          {
               narr[i]=arr[i];
          }
          delete []arr;
          size=nsize;
          arr=narr;
     }
     arr[++top]=foo;
     return;
}


ERROR MSG
-=-=-
convert.cpp:12: cannot resolve overloaded function `peek' based on conversion
   to type `char'
So below is my stack.h.  But when I declare a stack called equation and do "har q=equation.peek;"it doesnt work.

stack.h
-=-=-
#include <iostream>
using namespace std;

template <class T>
class stack
{
     public:
          stack();
          ~stack();
          bool empty() const;
          bool full() const;
          void push(const T);
          T pop();
          T peek() const;
     private:
          T *arr;
          int size;
          int top;
          static const int init_size=5;
};

template<class T>
stack<T>::stack()
{
     size=init_size;
     arr=new T[size];
     top=-1;
     return;
}

template<class T>
stack<T>::~stack()
{
     delete []arr;
     return;
}

template<class T>
bool stack<T>::empty() const
{
     return (top<0);
}

template<class T>
bool stack<T>::full() const
{
     return (top>=(size-1));
}

template<class T>
T stack<T>::peek() const
{
     T rval;
     if(empty())
     {
          cerr << "peek error" << endl;
     }else
     {
          rval=arr[top];
     }
     return rval;
}

template<class T>
T stack<T>::pop()
{
     T rval=peek();
     --top;
     return rval;
}

template<class T>
void stack<T>::push(const T foo)
{
     if(full())
     {
          int nsize=size+init_size;
          T *narr=new T[nsize];
          assert(narr);
          for(int i=0; i<size; ++i)
          {
               narr[i]=arr[i];
          }
          delete []arr;
          size=nsize;
          arr=narr;
     }
     arr[++top]=foo;
     return;
}


ERROR MSG
-=-=-
convert.cpp:12: cannot resolve overloaded function `peek' based on conversion
   to type `char'
you have:
class stack
{
    public:
         stack();
         ~stack();
         bool empty() const;
         bool full() const;
         void push(const T);
         T pop();
         T peek() const;
    private:
         T *arr;
         int size;
         int top;
         static const int init_size=5;
};
in the last line, it's an error: static variables cannot be instantiated in the class !!! do this:

class stack
{
    public:
         stack();
         ~stack();
         bool empty() const;
         bool full() const;
         void push(const T);
         T pop();
         T peek() const;
    private:
         T *arr;
         int size;
         int top;
         static const int init_size; //=5;
};

template <class T>
const int stack<T>::init_size = 5;

In this way it's ok. i tested the class and it's don't have any errors. probably the error with the peek() method is in another place (check the source file where you use the template class)
ASKER CERTIFIED SOLUTION
Avatar of cmaryus
cmaryus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First off, I suggest you just drop stack.cpp. If you really want to have a separate file with the function definitions for the template you can have a file stack.tcc or whatever you wanna call it, gcc's STL uses '.tcc' as file extension for files like this, another option might be '.tpp' and then #include that file from your stack.h file. so at the bottom of stack.h you do #include "stack.tcc".

Secondly, I see one error here in that you have:

T rval = 0;

Since rval is of an unknown type we cannot know if 0 is an appropriate value for that type. I would suggest you use T() instead which indicate a default constructor of type T. If T is char it will result in '\0', if T is wchar_t it will result in L'\0', if T is int it will be 0, if T is double it is 0.0 if T is a pointer (including char *) it will be a null pointer etc. For user defined classes it will use the default constructor.

so:

T rval = T();

Other than that your class seem ok, I admit that I haven't checked it very carefully so if you still have problems tell us about it and we'll get back to you.

Alf
Alf,

Just for your information: he's removed the '= 0' part from 'T rval ;' in the updated code.

Cheers,

Mayank.
Alright, so I am still stuck

gpp -Wall -c convert.cpp
convert.cpp: In function `int main()':
convert.cpp:12: cannot resolve overloaded function `peek' based on conversion
   to type `char'
make.exe: *** [convert.o] Error 1


convert.cpp(main)
-=-=-=-
//Includes
#include "mystack.h"
#include <iostream>
using namespace std;

//Main function
int main()
{
     mystack <char>equation;

     equation.push('c');
     char q=equation.peek;
     cout << q;

     return 0;
}
>> char q=equation.peek;

char q = equation.peek () ;

Mayank.

peek is a method, so just 'peek' resolves to a pointer to member function, this isn't what you want. You want to call the method and get the return value from it, so - as Mayank says, add the () after peek.

Alf
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered: Points split between cmaryus and mayankeagle

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer

Yup, split.