?
Solved

Templated stack

Posted on 2003-03-24
16
Medium Priority
?
502 Views
Last Modified: 2010-04-01
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
Comment
Question by:blaksaga
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +3
16 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 8200235
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
 

Author Comment

by:blaksaga
ID: 8200265
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
 

Author Comment

by:blaksaga
ID: 8200297
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
Independent Software Vendors: 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!

 

Author Comment

by:blaksaga
ID: 8200410
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
 

Author Comment

by:blaksaga
ID: 8200438
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
 
LVL 3

Expert Comment

by:cmaryus
ID: 8200683
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
 
LVL 3

Accepted Solution

by:
cmaryus earned 100 total points
ID: 8200709
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
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 100 total points
ID: 8200711
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
 
LVL 12

Expert Comment

by:Salte
ID: 8202165
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8202187
Alf,

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

Cheers,

Mayank.
0
 

Author Comment

by:blaksaga
ID: 8207890
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8207940
>> char q=equation.peek;

char q = equation.peek () ;

Mayank.

0
 
LVL 12

Expert Comment

by:Salte
ID: 8209061
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
 
LVL 11

Expert Comment

by:bcladd
ID: 9558762
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 9578911
Yup, split.
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question