fork(2) download
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4.  
  5. template <typename T>
  6. class Array
  7. {
  8. public:
  9. explicit Array(size_t size, const T& value = T()): size_(size), memory_(::operator new(size_*sizeof(T))
  10. {
  11. elements = new (memory_) T[size_];
  12. std::uninitialized_fill(elements, elements+size_, value);
  13. }
  14. // конструктор класса, который создает Array размера size, заполненный значениями value типа T.
  15. // Если у класса нет конструктора по умолчанию, то второй аргумент этого конструктора обязателен.
  16.  
  17. Array(): size_(0), elements(new T[0]){}
  18. // конструктор класса, который можно вызвать без параметров. Должен создавать пустой Array.
  19.  
  20. Array(const Array& other): size_(other.size_), memory_(::operator new (size_*sizeof(T)))
  21. {
  22. elements = new (memory_) T[size_];
  23. std::uninitialized_copy(other.elements, other.elements + size_, elements);
  24. }
  25. // конструктор копирования, который создает копию параметра. Для типа T оператор присваивания не определен.
  26.  
  27. ~Array()
  28. {
  29. for(size_t i = 0; i!=size_;++i)
  30. elements[i].~T();
  31. ::operator delete[](elements);
  32. }
  33. // деструктор, если он вам необходим.
  34.  
  35. Array& operator=(const Array& other)
  36. {
  37. if(this==&other) return *this;
  38.  
  39. for(size_t i = 0; i!=size_;++i)
  40. elements[i].~T();
  41. ::operator delete[](memory_);
  42.  
  43. memory_ = ::operator new(other.size_);
  44. elements = new (memory_) T[other.size_];
  45. std::uninitialized_copy(other.elements, other.elements + size_, elements);
  46. size_ = other.size_;
  47. return *this;
  48. }
  49. // оператор присваивания.
  50.  
  51. size_t size() const { return size_; }
  52. // возвращает размер массива (количество элементов).
  53.  
  54. T& operator[](size_t index) { return elements[index]; }
  55. const T& operator[](size_t index) const { return elements[index]; }
  56. // две версии оператора доступа по индексу.
  57. private:
  58. size_t size_;
  59. char* memory_;
  60. T* elements;
  61. };
  62.  
  63. int main() {
  64. Array<float> ololo{};
  65. Array<float> psh(12, 321.099f);
  66. std::cout << "psh[10] = " << psh[10] << '\n';
  67. return 0;
  68. }
Compilation error #stdin compilation error #stdout 0s 4516KB
stdin
Standard input is empty
compilation info
prog.cpp: In constructor ‘Array<T>::Array(size_t, const T&)’:
prog.cpp:9:56: error: class ‘Array<T>’ does not have any field named ‘size_’
     explicit Array(size_t size, const T& value = T()): size_(size), memory_(::operator new(size_*sizeof(T))
                                                        ^~~~~
prog.cpp:9:92: error: ‘size_’ was not declared in this scope
     explicit Array(size_t size, const T& value = T()): size_(size), memory_(::operator new(size_*sizeof(T))
                                                                                            ^~~~~
prog.cpp:17:42: error: expected ‘)’ before ‘{’ token
     Array(): size_(0), elements(new T[0]){}
                                          ^
prog.cpp:21:5: error: expected ‘)’ before ‘{’ token
     {
     ^
prog.cpp:58:12: error: expected ‘{’ at end of input
     size_t size_;
            ^~~~~
prog.cpp: In function ‘int main()’:
prog.cpp:64:21: error: no matching function for call to ‘Array<float>::Array(<brace-enclosed initializer list>)’
  Array<float> ololo{};
                     ^
prog.cpp:9:14: note: candidate: Array<T>::Array(size_t, const T&) [with T = float; size_t = long unsigned int]
     explicit Array(size_t size, const T& value = T()): size_(size), memory_(::operator new(size_*sizeof(T))
              ^~~~~
prog.cpp:9:14: note:   candidate expects 2 arguments, 0 provided
prog.cpp:6:7: note: candidate: constexpr Array<float>::Array(const Array<float>&)
 class Array
       ^~~~~
prog.cpp:6:7: note:   candidate expects 1 argument, 0 provided
prog.cpp:6:7: note: candidate: constexpr Array<float>::Array(Array<float>&&)
prog.cpp:6:7: note:   candidate expects 1 argument, 0 provided
prog.cpp:66:34: error: no match for ‘operator[]’ (operand types are ‘Array<float>’ and ‘int’)
  std::cout << "psh[10] = " << psh[10] << '\n';
                                  ^
stdout
Standard output is empty