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.
//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 ‘constchar*’ and ‘constchar [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 for ‘operator=’ 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);
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;
}
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.
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.
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!
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).