fork download
  1. #ifndef CircularDequeTemplate
  2. #define CircularDequeTemplate
  3. using namespace std;
  4.  
  5. template<class T> class CirDequeTemplate;
  6. template<class T> std::ostream& operator<< (std::ostream& o, const CirDequeTemplate<T>& q);
  7.  
  8.  
  9. template <class T>
  10. class CirDequeTemplate {
  11.  
  12. private:
  13. T data[10];
  14. int front, rear;
  15.  
  16. public:
  17. CirDequeTemplate() { rear = front = 9; };
  18. void push_front(T); // push an element to the front of the deque
  19. void push_back(T); // push an element to the end of the deque
  20. T pop_front(); // pop an element from the front of the deque
  21. T pop_back(); // pop an element from the end of the deque
  22. friend ostream& operator<< <>(ostream&, const CirDequeTemplate&);
  23. // print the elements in queue from front to rear and separate each element by a space.
  24. };
  25. // you may write your code below this line
  26.  
  27. template<class T>
  28. std::ostream & operator<< (std::ostream &s, const CirDequeTemplate<T> &cird)
  29. {
  30. for (int i = cird.front + 1; i <= cird.rear; i++)
  31. {
  32. if (i == cird.front + 1) printf("%d", cird.data[i]);
  33. else printf(" %d", cird.data[i]);
  34. }
  35. return s;
  36. }
  37. template<class T>
  38. inline void CirDequeTemplate<T>::push_front(T a)
  39. {
  40. if (rear - front == 9) { printf("queue is full\n"); return; }
  41. if (front == 0)
  42. {
  43. for (int i = rear; i >= 0; i--)data[i + 1] = data[i];
  44. front++;
  45. rear++;
  46. }
  47. data[front--] = a;
  48. }
  49. template<class T>
  50. inline void CirDequeTemplate<T>::push_back(T a)
  51. {
  52. if (rear - front == 9) { printf("queue is full\n"); return; }
  53. if (rear == 9)
  54. {
  55. for (int i = front; i <= 9; i++)data[i - 1] = data[i];
  56. rear--;
  57. }
  58. data[++rear] = a;
  59. front--;
  60. }
  61. template<class T>
  62. inline T CirDequeTemplate<T>::pop_front()
  63. {
  64. if (rear == front) { printf("queue is empty\n"); return 0; }
  65. front++;
  66. return data[front];//
  67. }
  68. template<class T>
  69. inline T CirDequeTemplate<T>::pop_back()
  70. {
  71. if (rear == front) { printf("queue is empty\n"); return 0; }
  72. rear--;
  73. return data[rear + 1];//+1
  74.  
  75. }
  76. // you may write your code above this line
  77. #endif
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:6:24: error: ‘ostream’ in namespace ‘std’ does not name a type
 template<class T> std::ostream& operator<< (std::ostream& o, const CirDequeTemplate<T>& q);
                        ^~~~~~~
prog.cpp:22:9: error: ‘ostream’ does not name a type
  friend ostream& operator<< <>(ostream&, const CirDequeTemplate&);
         ^~~~~~~
prog.cpp:28:6: error: ‘ostream’ in namespace ‘std’ does not name a type
 std::ostream &  operator<< (std::ostream &s, const CirDequeTemplate<T> &cird)
      ^~~~~~~
prog.cpp: In member function ‘void CirDequeTemplate<T>::push_front(T)’:
prog.cpp:40:51: error: there are no arguments to ‘printf’ that depend on a template parameter, so a declaration of ‘printf’ must be available [-fpermissive]
  if (rear - front == 9) { printf("queue is full\n"); return; }
                                                   ^
prog.cpp:40:51: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
prog.cpp: In member function ‘void CirDequeTemplate<T>::push_back(T)’:
prog.cpp:52:51: error: there are no arguments to ‘printf’ that depend on a template parameter, so a declaration of ‘printf’ must be available [-fpermissive]
  if (rear - front == 9) { printf("queue is full\n"); return; }
                                                   ^
prog.cpp: In member function ‘T CirDequeTemplate<T>::pop_front()’:
prog.cpp:64:48: error: there are no arguments to ‘printf’ that depend on a template parameter, so a declaration of ‘printf’ must be available [-fpermissive]
  if (rear == front) { printf("queue is empty\n"); return 0; }
                                                ^
prog.cpp: In member function ‘T CirDequeTemplate<T>::pop_back()’:
prog.cpp:71:48: error: there are no arguments to ‘printf’ that depend on a template parameter, so a declaration of ‘printf’ must be available [-fpermissive]
  if (rear == front) { printf("queue is empty\n"); return 0; }
                                                ^
stdout
Standard output is empty