fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int DefaultSize = 10;
  5.  
  6. class Animal
  7. {
  8. public:
  9. Animal(int);
  10. Animal();
  11. ~Animal() { }
  12.  
  13. int GetWeight() const { return itsWeight; }
  14. void Display() const { cout << itsWeight; }
  15.  
  16. private:
  17. int itsWeight;
  18.  
  19. };
  20.  
  21. Animal::Animal(int weight): itsWeight(weight)
  22. { }
  23.  
  24. Animal::Animal(): itsWeight(0)
  25. { }
  26.  
  27. template <class T>
  28. class Array
  29. {
  30. public:
  31. Array(int itsSize = DefaultSize);
  32. Array( Array &rhs);
  33. ~Array()
  34. {
  35. delete [] pType;
  36. }
  37.  
  38. Array& operator= (const Array&);
  39. T& operator[] (int offset)
  40. {
  41. return pType[offset];
  42. }
  43.  
  44. int GetSize () const
  45. {
  46. return itsSize;
  47. }
  48. template< class U >
  49. // friend
  50. ostream& operator<< (ostream&, Array<U>&);
  51.  
  52. private:
  53. T *pType;
  54. int itsSize;
  55. };
  56.  
  57.  
  58. template <class T>
  59. ostream& operator<< (ostream& output, Array<T>& theArray)
  60. {
  61. for (int i = 0; i < theArray.GetSize(); ++i)
  62. {
  63. output << "[" << i << "]" << theArray[i] << endl;
  64. }
  65. return output;
  66. }
  67.  
  68.  
  69. template <class T>
  70. Array<T>::Array(int size): itsSize(size)
  71. {
  72. pType = new T[size];
  73. for (int i = 0; i < size; ++i)
  74. pType[i] = 0;
  75. }
  76.  
  77. //конструктор копирования
  78. template <class T>
  79. Array<T>::Array(Array &rhs)
  80. {
  81. itsSize = rhs.GetSize();
  82. pType = new T[itsSize];
  83. for (int i = 0; i < itsSize; ++i)
  84. {
  85. pType[i] = rhs[i];
  86. }
  87. }
  88.  
  89. //оператор присваения
  90. template <class T>
  91. Array<T>& Array<T>::operator= (const Array &rhs)
  92. {
  93. if (this == &rhs)
  94. return *this;
  95.  
  96. delete [] pType;
  97. itsSize = rhs.GetSize();
  98. pType = new T[itsSize];
  99.  
  100. for (int i = 0; i < itsSize; ++i)
  101. {
  102. pType[i] = rhs[i];
  103. }
  104.  
  105. return *this;
  106. }
  107.  
  108. ///////////////////////////////////////////////////////////////////
  109.  
  110. int main()
  111. {
  112. bool Stop = false;
  113. int offset, value;
  114. Array<int> theArray;
  115.  
  116. while (!Stop)
  117. {
  118. cout << "Enter an offset (0-9) ";
  119. cout << "and a value. (-1 to stop): ";
  120. cin >> offset >> value;
  121.  
  122. if (offset < 0)
  123. break;
  124.  
  125. if(offset > 9)
  126. {
  127. cout << "***Please use value between 0 and 9*** \n";
  128. continue;
  129. }
  130.  
  131. theArray[offset] = value;
  132. }
  133. cout << "\n Here's the entire array: \n";
  134. cout << theArray << endl;
  135.  
  136.  
  137. return 0;
  138. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
3 2
-1 0
compilation info
prog.cpp:50:41: error: ‘std::ostream& Array<T>::operator<<(std::ostream&, Array<U>&)’ must take exactly one argument
 ostream& operator<< (ostream&, Array<U>&);
                                         ^
stdout
Standard output is empty