fork download
  1. #include "iostream"
  2.  
  3. using namespace std;
  4.  
  5. template < class T> class stack
  6. {
  7. template <class T> struct item { item * prev; T data; };
  8. public:
  9. stack() { count=0;}
  10. item<T> * it;
  11. T toItem(T);
  12. T outItem();
  13. T pop();
  14. T slow();
  15. int count;
  16. };
  17.  
  18. template <class T> T stack<T>::toItem(T elem)
  19. {
  20. item <T>* el= new item<T>;
  21. if(count==0) { el->data= elem; el->prev=0; it=el; count++; }
  22. else { el->data= elem; el->prev=it; it=el; }
  23. return 0;
  24. }
  25.  
  26. template <class T> T stack <T>::outItem()
  27. {
  28. item<T> * el= new item<T>; el=it; cout << el->data << endl;
  29. for(;el->prev!=0; el=el->prev) { cout << el->data << endl; }
  30. return 0;
  31. }
  32.  
  33. template<class T> T stack<T>::pop() { item<T> *l= it->prev; delete [] it; it=l; count--; return 0; }
  34. template<class T> T stack<T>::slow()
  35. {
  36. stack();
  37. for(int i=0; i<5; i++) { toItem(rand()%100); }
  38. outItem(); pop(); return 0;
  39. }
  40.  
  41. int main()
  42. {
  43. stack<int> p; p.slow();
  44. return 0;
  45. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:7: error: declaration of ‘class T’
prog.cpp:5: error:  shadows template parm ‘class T’
prog.cpp: In member function ‘T stack<T>::slow()’:
prog.cpp:37: error: there are no arguments to ‘rand’ that depend on a template parameter, so a declaration of ‘rand’ must be available
prog.cpp:37: error: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
prog.cpp: In member function ‘T stack<T>::slow() [with T = int]’:
prog.cpp:43:   instantiated from here
prog.cpp:37: error: ‘rand’ was not declared in this scope
stdout
Standard output is empty