Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

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;
}
0
blaksaga
Asked:
blaksaga
  • 5
  • 4
  • 2
  • +3
2 Solutions
 
mnashadkaCommented:
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!
0
 
blaksagaAuthor Commented:
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!
0
 
blaksagaAuthor Commented:
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!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
blaksagaAuthor Commented:
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'
0
 
blaksagaAuthor Commented:
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'
0
 
cmaryusCommented:
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)
0
 
cmaryusCommented:
Also because the stack class is already a STL class you have 2 options:
1. remove the "using namespace std" line
2. give your class a diferent name, mystack for example

also, because is a template, the declaration& implementation of the class must be in the
header file.

So you will have a header file, let's say mystack.h

1.

#include <iostream.h>

// the rest of you code...
template <class T>
class stack()...
..

2.
#include <iostream>
using namespace std;

template <class T>
class mystack
// the rest of the implementation but for class mystack
...
0
 
Mayank SAssociate Director - Product EngineeringCommented:
First of all, you have included the stack.h file in both, stack.cpp and convert.cpp, but you haven't included stack.cpp in convert.cpp, so I wonder where will the compiler look for your function definitions. So you should include stack.cpp in convert.cpp (instead of stack.h) and let stack.cpp stay as it is (and it will automatically include stack.h).

Second, I agree with mnashadka that you should rename your class to something else, since there is one stack class already there in the library. Change it to, say, MyStack.

Next, I don't see any code of convert.cpp except for the declaration of the stack object, so please post your updated codes so that we can have a look at it and try to solve the problem. The line giving the error in convert.cpp is itself not there on this page.

Mayank.
0
 
SalteCommented:
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
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Alf,

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

Cheers,

Mayank.
0
 
blaksagaAuthor Commented:
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;
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> char q=equation.peek;

char q = equation.peek () ;

Mayank.

0
 
SalteCommented:
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
0
 
bcladdCommented:
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

0
 
Mayank SAssociate Director - Product EngineeringCommented:
Yup, split.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now