hello,
I'm trying to compile this piece of code from here,
http://www.josuttis.com/cppbook/tmpl/stest5.cpp.html
instead of stack5.hpp I inserted
stack5decl.hpp <http://www.josuttis.com/cppbook/tmpl/stack5decl.hpp.html>
stack5assign.hpp <http://www.josuttis.com/cppbook/tmpl/stack5assign.hpp.html>
Please help.
I'm using gcc under solaris,
bash-2.05$ g++ -v
Reading specs from /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/specs
Configured with: ../gcc-3.3.2/configure --prefix=/opt/sfw/gcc-3 --with-ld=/usr/ccs/bin/ld --with-as=/usr/ccs/bin/as --without-gnu-ld --without-gnu-as --enable-shared
Thread model: posix
gcc version 3.3.2
g++ -ansi -pedantic -Wall -o stest5_test_00.out stest5_test_00.cpp -L /opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++
stest5_test_00.cpp:11: error: syntax error before `;' token
#include <iostream> #include <string> #include <cstdlib>
// #include "stack5.hpp"
template <typename T> class Stack { private: std::deque<T> elems; // elements
public: void push(const T&); // store new top element void pop(); // remove top element T top() const; // return top element bool empty() const { // return whether the stack is empty return elems.empty(); }
// assign stack of elements of type T2 template <typename T2> Stack<T>& operator= (Stack<T2> const&); };
template <typename T> template <typename T2> Stack<T>& Stack<T>::operator= (const Stack<T2>& op2) { if ((void*)this == (void*)&op2) { // assignment to itself? return *this; }
Stack<T2> tmp(op2); // create a copy of the assigned stack
elems.clear(); // remove existing elements while (!tmp.empty()) { // copy all elements elems.push_front(tmp.top()); tmp.pop(); } return *this; }
int main() { try { Stack<int> intStack; // stack of ints Stack<float> floatStack; // stack of floats
// manipulate int stack intStack.push(42); intStack.push(7);
// manipulate float stack floatStack.push(7.7);
// assign stacks of different type floatStack = intStack;
// print float stack std::cout << floatStack.top() << std::endl; floatStack.pop(); std::cout << floatStack.top() << std::endl; floatStack.pop(); std::cout << floatStack.top() << std::endl; floatStack.pop(); } catch (std::exception const& ex) { std::cerr << "Exception: " << ex.what() << std::endl; return EXIT_FAILURE; // exit program with ERROR status } }
best regards
Morten