Stack class help

Hey everyone,

So I am writing a Stack class to convert a number to a base of their choice, 2-16. I am okay with that part of the assignment, but when it comes to flipping the result, I am getting more errors than I can handle. Obviously, I am not implementing my flip method correctly, so I would like to know how to fix it.

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Stack.hpp
#include <iostream>
#include <vector>
#include <cstdlib>

using std::vector;

template<typename T>

class Stack {

public:
 Stack () : v ()
 {
 }

 T& top ()
 {
   return v.back ();
 }

 void push (const T& i)
 {
   v.push_back (i);
 }

 void pop ()
 {
   v.pop_back ();
  }

 bool empty () const
 {
   return  v.empty ();
 }

 size_t size () const
 {
   return v.size ();
 }

 void flip ()
  {
    Stack temp = new Stack ();
    while (! v.empty ())
      {
	temp.push(v.pop());
      }
    v = temp;
  }


private:
 vector<T> v;
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//Stack.cc

// System includes

#include <iostream>
#include <string>
#include <cstdlib>

/************************************************************/
// Local includes
#include "Stack.hpp"

/************************************************************/
// Using declarations

using std::cout;
using std::cin;
using std::string;
using std::endl;

/************************************************************/

int      
main (int argc, char* argv[]) 
{        

 int num;
 int base;
 string hex = ("0123456789ABCDEF");

 cout << "Number ==> ";
 cin >> num;
 cout << "Base (Range: [2-16]) ==> ";
 cin >> base;

 Stack <char> s;
 int result;

 while (num != 0)
   {
     result = num % base;
     s.push(hex[result]);
     num = num/base;
   }

 string stackResult;
 char topChar;
 
 while (! s.empty())
   {
     topChar = s.top();
     stackResult += topChar;
     s.pop();
   }

 string flippedStack = s.flip();

 cout << "Converted number: [ " + stackResult + " ]" << endl;

 cout << "Stack flipped: [ " + flippedStack + " ] " << endl;


 return EXIT_SUCCESS;
}

/************************************************************/
/************************************************************/


Here's the list of errors I am getting that I looked up, but could not figure out how to fix:

1
2
3
4
5
6
7
8
9
10
11
12
Stack.cc: In function ‘int main(int, char**)’:
Stack.cc:54:29: error: void value not ignored as it ought to be
Stack.cc:58:47: error: invalid operands of types ‘const char*’ and ‘const char [4]’ to binary ‘operator+’
In file included from Stack.cc:9:0:
Stack.hpp: In member function ‘void Stack<T>::flip() [with T = char]’:
Stack.cc:54:29:   instantiated from here
Stack.hpp:43:29: error: conversion from ‘Stack<char>*’ to non-scalar type ‘Stack<char>’ requested
Stack.hpp:46:2: error: ‘class std::vector<char, std::allocator<char> >’ has no member named ‘pop’
Stack.hpp:48:5: error: no match foroperator=’ in ‘((Stack<char>*)this)->Stack<char>::v = temp’
Stack.hpp:48:5: note: candidate is:
/usr/include/c++/4.6/bits/vector.tcc:161:5: note: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = char, _Alloc = std::allocator<char>]
/usr/include/c++/4.6/bits/vector.tcc:161:5: note:   no known conversion for argument 1 from ‘Stack<char>’ to ‘const std::vector<char, std::allocator<char> >&’




Also, could anybody explain to me ostream&? We were told to use it for this assignment, I assume to separate the elements in the formatting, but I am very new to C++ and I have never had to use ostream& before. Just a general how-to for it would really help! Here's how it looks on the assignment:

1
2
3
4
// Output elements from top to bottom in following form:
//   [ e1 e2 e3 ]
ostream&
operator<< (ostream& os, const Stack& s);



Thank you so much for any help!
Last edited on
Hey, just wondering if anyone could help me with this. I am still stuck unfortunately. =/
42
43
44
45
46
47
48
49
50
 void flip ()
  {
    Stack temp = new Stack ();
    while (! v.empty ())
      {
	temp.push(v.pop());
      }
    v = temp;
  }
C++ is not Java. You should just write Stack temp; and not use the word "new" at all.

Also maybe you want temp to be a std::vector<T> and not a Stack.
Last edited on
You already have the contents of the stack in a string so if you just need to output it flipped you can use std::string::reverse_iterator. See example here: http://www.cplusplus.com/reference/string/string/rbegin/
EDIT: See discussion below.

If you need to flip the actual stack you're going to have to make a copy because you are popping values off of the stack to get the first string (stackResult) leaving the stack empty. So you need a copy constructor and/or an operator=() for your Stack class. Then flip() could look something like this:
1
2
3
4
5
6
 void flip() {
     vector<T> temp;
     for(typename vector<T>::reverse_iterator it = v.rbegin(); it != v.rend(); ++it)
         temp.push_back(*it);
     v = temp;
  }


HTH
Last edited on
@norm b: I don't know how you involved std::string with this when the problem is not in his client code.
@L B:
1
2
3
4
5
6
7
8
9
string stackResult;
 char topChar;
 
 while (! s.empty())
   {
     topChar = s.top();
     stackResult += topChar;
     s.pop();
   }
His stack class is a template, the data contained may not be reversible simply by reversing its serialized string form. While it may work in this particular case it is more important to teach a solution that works in as many cases as possible.
Last edited on
You're right, that didn't occur to me. I'll strike that.
After
1
2
3
4
5
6
while (! s.empty())
   {
     topChar = s.top();
     stackResult += topChar;
     s.pop();
   }

your Stack is empty. You won't have anything to flip.

@LB His template class is using a vector as the container, so what is the issue with norm b's flip solution. Its not the container that is templated, its what is contained so revisable is always going to be available.
@histrungalot: norm b's solution(s) involved strings.
Oh, passed over the stricken text. My bad.
Thanks for the help, everyone! I am still getting the error of converting from void to non-scalar type std::string. Am I supposed to implement flip as a string in my Stack.cc or not? I'd like to get the code to compile with the flip method without any errors just so I can work on formatting the output. Any help with implementing flip would help greatly!
string flippedStack = s.flip();

This won't work. Instead you need to call s.flip(); by itself and then make a string out of the characters in the stack again (or just print out the characters directly).
So am I essentially calling s.flip(); and then running through in the same way that I ran through for stackResult?
Topic archived. No new replies allowed.