Why this simple Stack does not work with string?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 16
Reputation: kevintse is an unknown quantity at this point 
Solved Threads: 0
kevintse kevintse is offline Offline
Newbie Poster

Why this simple Stack does not work with string?

 
0
  #1
Dec 15th, 2008
  1. #include <iostream>
  2. using namespace std;
  3. #define OUT(x) cout << (x) << endl;
  4.  
  5. template<class T, int size> class Stack{
  6. T arr[size];
  7. int top;
  8. public:
  9. Stack():top(-1){}
  10. void push(T obj);
  11. T pop();
  12. };
  13. template<class T, int size> void Stack<T,size>::push(T obj){
  14. top++;
  15. if(top >= size){
  16. top--;
  17. OUT("the stack is full");
  18. }else{
  19. arr[top] = obj;
  20. }
  21. }
  22. template<class T, int size> T Stack<T,size>::pop(){
  23. if(top < 0){
  24. OUT("the stack is empty");
  25. return NULL;
  26. }else{
  27. T obj = arr[top];
  28. arr[top] == NULL;
  29. top--;
  30. return obj;
  31. }
  32. }
  33.  
  34. int main() {
  35. Stack<int, 5> intStack;
  36. for(int i = 0; i < 5; i++)
  37. intStack.push(i);
  38. for(int i = 0; i < 5; i++)
  39. OUT(intStack.pop())
  40.  
  41. return 0;
  42. }

The code above works fine without any compile time or runtime errors, but when i add this to the main function:
  1. Stack<string, 5> strStack;
  2. for(int i = 0; i < 5; i++)
  3. strStack.push("hello");
  4. for(int i = 0; i < 5; i++)
  5. OUT(strStack.pop())
and when i compile the code, it gives me these error messages:
(i test this on eclipse)
make all
Building file: ../src/Stack.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Stack.d" -MT"src/Stack.d" -o"src/Stack.o" "../src/Stack.cpp"
../src/Stack.cpp: In member function ‘T Stack<T, size>::pop() [with T = int, int size = 5]’:
../src/Stack.cpp:39: instantiated from here
../src/Stack.cpp:28: warning: NULL used in arithmetic
../src/Stack.cpp:28: warning: statement has no effect
../src/Stack.cpp: In member function ‘T Stack<T, size>::pop() [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int size = 5]’:
../src/Stack.cpp:45: instantiated from here
../src/Stack.cpp:28: error: no match for ‘operator==’ in ‘((Stack<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 5>*)this)->Stack<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 5>::arr[((Stack<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 5>*)this)->Stack<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 5>::top] == 0’
make: *** [src/Stack.o] Error 1
Last edited by kevintse; Dec 15th, 2008 at 11:48 am.
A little progress a day is what life has to be about.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why this simple Stack does not work with string?

 
1
  #2
Dec 15th, 2008
Start by including the <string> header, which you failed to do. Also note that NULL isn't a compatible type with std::string; your stack class makes unwarranted assumptions about the type of T. Instead of NULL, you can generally use T() safely as a default value, though it's usually a better idea to design your class so as to not need a default value.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: kevintse is an unknown quantity at this point 
Solved Threads: 0
kevintse kevintse is offline Offline
Newbie Poster

Re: Why this simple Stack does not work with string?

 
0
  #3
Dec 15th, 2008
Thank you for your reply,

When I change this line arr[top] == NULL; to arr[top] == T(); , it just works. but this seems not being what I wanted, I wanted to assign a "null" value to the array element, T() does not produce a "null" value, right? I mean "null", something we find in Java. is there an equivalent in C++?
Last edited by kevintse; Dec 15th, 2008 at 1:10 pm.
A little progress a day is what life has to be about.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Why this simple Stack does not work with string?

 
0
  #4
Dec 15th, 2008
Originally Posted by Narue View Post
...though it's usually a better idea to design your class so as to not need a default value.
It's not so easily to design an intrusive container class for elements without default constructor (no underlying arrays, for example).
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why this simple Stack does not work with string?

 
1
  #5
Dec 15th, 2008
>T() does not produce a "null" value, right?
It produces a default constructed object for the type. In the case of std::string, it's an empty string.

>I wanted to assign a "null" value to the array element
Assigning "null" doesn't do anything meaningful in this case. What are you trying to accomplish?

>is there an equivalent in C++?
It depends on what you want to do. If you're worried about any of the cases where setting to "null" is useful, you've got fundamental design issues to iron out first. Personally, I don't see why it can't be as simple as this for your class:
  1. #include <iostream>
  2. #include <exception>
  3. #include <stdexcept>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. template <class T, int size>
  9. class Stack {
  10. T arr[size];
  11. int top;
  12. public:
  13. Stack(): top(0) {}
  14. void push ( T obj );
  15. T pop();
  16. };
  17.  
  18. template <class T, int size>
  19. void Stack<T,size>::push ( T obj )
  20. {
  21. if ( top >= size )
  22. throw runtime_error ( "Stack is full" );
  23.  
  24. arr[top++] = obj;
  25. }
  26.  
  27. template <class T, int size>
  28. T Stack<T,size>::pop()
  29. {
  30. if ( top == 0 )
  31. throw runtime_error ( "Stack is empty" );
  32.  
  33. return arr[--top];
  34. }
  35.  
  36. int main()
  37. {
  38. Stack<string, 5> s;
  39.  
  40. try {
  41. for ( int i = 0; i < 6; i++ )
  42. s.push ( "hello" );
  43. } catch ( exception& ex ) {
  44. cerr<< ex.what() <<'\n';
  45. }
  46.  
  47. try {
  48. for ( int i = 0; i < 6; i++ )
  49. cout<< s.pop() <<'\n';
  50. } catch ( exception& ex ) {
  51. cerr<< ex.what() <<'\n';
  52. }
  53. }
>It's not so easily to design an intrusive container class
>for elements without default constructor
Either you misunderstood what I said, or I have no idea what point you're trying to make.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: kevintse is an unknown quantity at this point 
Solved Threads: 0
kevintse kevintse is offline Offline
Newbie Poster

Re: Why this simple Stack does not work with string?

 
0
  #6
Dec 15th, 2008
It produces a default constructed object for the type. In the case of std::string, it's an empty string.
so the default constructed object of string class does not take storage? this is what I care about, when I pop an element out of the Stack, I don't want that element to still take memory storage.
A little progress a day is what life has to be about.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why this simple Stack does not work with string?

 
1
  #7
Dec 15th, 2008
>so the default constructed object of string class does not take storage?
It's an empty string, for whatever that means on your system. Most likely that means the base size for the object and no wasted dynamic memory.

>when I pop an element out of the Stack, I don't
>want that element to still take memory storage.
My advice is not to worry about it. But setting the item to T() is the most portable way to hopefully do what you want, if you're lucky. Will it work? Maybe, maybe not, depending on the type of T and how much extra dynamic memory it uses for the default construction. Will it hurt? Maybe, maybe not, also depending on the type of T and how it handles memory.

There's just so much involved that unless you've done this before and have a good idea of how things will work, my advice once again is not to worry about it.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: kevintse is an unknown quantity at this point 
Solved Threads: 0
kevintse kevintse is offline Offline
Newbie Poster

Re: Why this simple Stack does not work with string?

 
0
  #8
Dec 16th, 2008
Narue, Thank you so much!
Now the confusion in my head has been cleared.
Thank you!
A little progress a day is what life has to be about.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC