fork download
  1. class Stack {
  2. public:
  3. Stack() = default;
  4. Stack(const Stack& other) :size(other.size) {
  5. this->data = new int[other.size];
  6. for (size_t i = 0; i < other.size; i++) {
  7. this->data[i] = other.data[i];
  8. }
  9. }
  10.  
  11. Stack& operator=(const Stack& x) {
  12. if (&x != this) {
  13. delete[] data;
  14.  
  15. this->data = new int[x.size];
  16. for (size_t i = 0; i < x.size; i++) {
  17. this->data[i] = x.data[i];
  18. }
  19. size = x.size;
  20. }
  21. return *this;
  22. }
  23.  
  24. Stack(Stack&& other) noexcept : size(other.size), data(other.data) {
  25. other.data = nullptr;
  26. other.size = 0;
  27. }
  28.  
  29. Stack& operator=(Stack&& x) noexcept {
  30. if (&x != this) {
  31. delete[] data;
  32. size = x.size;
  33. data = x.data;
  34.  
  35. x.data = nullptr;
  36. x.size = 0;
  37.  
  38. }
  39. return *this;
  40. }
  41. void setSize(size_t size) {
  42. this->size = size;
  43. }
  44. void push(int element) {
  45. int* copy_arr = new int[size + 1];
  46. for (size_t i = 0; i < size; i++) {
  47. copy_arr[i + 1] = data[i];
  48. }
  49. copy_arr[0] = element;
  50. size++;
  51. delete[] data;
  52. data = copy_arr;
  53. }
  54. void pop() {
  55. int* copy_arr = new int[size - 1];
  56. for (size_t i = 0; i < size - 1; i++) {
  57. copy_arr[i] = data[i + 1];
  58. }
  59. size--;
  60. delete[] data;
  61. data = copy_arr;
  62. }
  63. void swap(Stack& other) {
  64. if (this == &other)
  65. return;
  66. size_t tmpSz = other.size;
  67. other.size = size;
  68. size = tmpSz;
  69. int* tmpData = other.data;
  70. other.data = data;
  71. data = tmpData;
  72. }
  73. int top() {
  74. return data[size-1];
  75. }
  76. bool isEmpty() {
  77. return size == 0;
  78. }
  79. int get(size_t index) {
  80. return data[index];
  81. }
  82. int getSize() {
  83. return size;
  84. }
  85.  
  86.  
  87. ~Stack() {
  88.  
  89. }
  90.  
  91. private:
  92. size_t size;
  93. int* data = new int[size];
  94. };
  95.  
  96. void print(Stack& stack) {
  97. for (size_t i = 0; i < stack.getSize(); i++) {
  98. std::cout.width(7);
  99. std::cout << stack.get(i);
  100. }
  101. std::cout << std::endl;
  102. }
  103.  
  104. int main(){
  105. Stack stack;
  106. stack.push(1);
  107. stack.push(2);
  108. stack.push(3);
  109.  
  110. print(stack);
  111. stack.pop();
  112. stack.pop();
  113. print(stack);
  114. std::cout.width(7);
  115. std::cout << stack.top();
  116. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:41:15: error: ‘size_t’ has not been declared
  void setSize(size_t size) {
               ^~~~~~
prog.cpp:79:10: error: ‘size_t’ has not been declared
  int get(size_t index) {
          ^~~~~~
prog.cpp:92:2: error: ‘size_t’ does not name a type
  size_t size;
  ^~~~~~
prog.cpp:92:2: note: ‘size_t’ is defined in header ‘<cstddef>’; did you forget to ‘#include <cstddef>’?
prog.cpp:1:1:
+#include <cstddef>
 class Stack {
prog.cpp:92:2:
  size_t size;
  ^~~~~~
prog.cpp:93:22: error: ‘size’ was not declared in this scope
  int* data = new int[size];
                      ^~~~
prog.cpp:93:22: note: suggested alternative: ‘setSize’
  int* data = new int[size];
                      ^~~~
                      setSize
prog.cpp: In copy constructor ‘Stack::Stack(const Stack&)’:
prog.cpp:4:29: error: class ‘Stack’ does not have any field named ‘size’
  Stack(const Stack& other) :size(other.size) {
                             ^~~~
prog.cpp:4:40: error: ‘const class Stack’ has no member named ‘size’; did you mean ‘setSize’?
  Stack(const Stack& other) :size(other.size) {
                                        ^~~~
                                        setSize
prog.cpp:5:30: error: ‘const class Stack’ has no member named ‘size’; did you mean ‘setSize’?
   this->data = new int[other.size];
                              ^~~~
                              setSize
prog.cpp:6:8: error: ‘size_t’ was not declared in this scope
   for (size_t i = 0; i < other.size; i++) {
        ^~~~~~
prog.cpp:6:8: note: ‘size_t’ is defined in header ‘<cstddef>’; did you forget to ‘#include <cstddef>’?
prog.cpp:6:22: error: ‘i’ was not declared in this scope
   for (size_t i = 0; i < other.size; i++) {
                      ^
prog.cpp:6:32: error: ‘const class Stack’ has no member named ‘size’; did you mean ‘setSize’?
   for (size_t i = 0; i < other.size; i++) {
                                ^~~~
                                setSize
prog.cpp: In member function ‘Stack& Stack::operator=(const Stack&)’:
prog.cpp:15:27: error: ‘const class Stack’ has no member named ‘size’; did you mean ‘setSize’?
    this->data = new int[x.size];
                           ^~~~
                           setSize
prog.cpp:16:9: error: ‘size_t’ was not declared in this scope
    for (size_t i = 0; i < x.size; i++) {
         ^~~~~~
prog.cpp:16:9: note: ‘size_t’ is defined in header ‘<cstddef>’; did you forget to ‘#include <cstddef>’?
prog.cpp:16:23: error: ‘i’ was not declared in this scope
    for (size_t i = 0; i < x.size; i++) {
                       ^
prog.cpp:16:29: error: ‘const class Stack’ has no member named ‘size’; did you mean ‘setSize’?
    for (size_t i = 0; i < x.size; i++) {
                             ^~~~
                             setSize
prog.cpp:19:4: error: ‘size’ was not declared in this scope
    size = x.size;
    ^~~~
prog.cpp:19:4: note: suggested alternative: ‘setSize’
    size = x.size;
    ^~~~
    setSize
prog.cpp:19:13: error: ‘const class Stack’ has no member named ‘size’; did you mean ‘setSize’?
    size = x.size;
             ^~~~
             setSize
prog.cpp: In constructor ‘Stack::Stack(Stack&&)’:
prog.cpp:24:34: error: class ‘Stack’ does not have any field named ‘size’
  Stack(Stack&& other) noexcept : size(other.size), data(other.data) {
                                  ^~~~
prog.cpp:24:45: error: ‘class Stack’ has no member named ‘size’; did you mean ‘setSize’?
  Stack(Stack&& other) noexcept : size(other.size), data(other.data) {
                                             ^~~~
                                             setSize
prog.cpp:26:9: error: ‘class Stack’ has no member named ‘size’; did you mean ‘setSize’?
   other.size = 0;
         ^~~~
         setSize
prog.cpp: In member function ‘Stack& Stack::operator=(Stack&&)’:
prog.cpp:32:4: error: ‘size’ was not declared in this scope
    size = x.size;
    ^~~~
prog.cpp:32:4: note: suggested alternative: ‘setSize’
    size = x.size;
    ^~~~
    setSize
prog.cpp:32:13: error: ‘class Stack’ has no member named ‘size’; did you mean ‘setSize’?
    size = x.size;
             ^~~~
             setSize
prog.cpp:36:6: error: ‘class Stack’ has no member named ‘size’; did you mean ‘setSize’?
    x.size = 0;
      ^~~~
      setSize
prog.cpp: In member function ‘void Stack::setSize(int)’:
prog.cpp:42:9: error: ‘class Stack’ has no member named ‘size’; did you mean ‘setSize’?
   this->size = size;
         ^~~~
         setSize
prog.cpp: In member function ‘void Stack::push(int)’:
prog.cpp:45:27: error: ‘size’ was not declared in this scope
   int* copy_arr = new int[size + 1];
                           ^~~~
prog.cpp:45:27: note: suggested alternative: ‘setSize’
   int* copy_arr = new int[size + 1];
                           ^~~~
                           setSize
prog.cpp:46:8: error: ‘size_t’ was not declared in this scope
   for (size_t i = 0; i < size; i++) {
        ^~~~~~
prog.cpp:46:8: note: ‘size_t’ is defined in header ‘<cstddef>’; did you forget to ‘#include <cstddef>’?
prog.cpp:46:22: error: ‘i’ was not declared in this scope
   for (size_t i = 0; i < size; i++) {
                      ^
prog.cpp: In member function ‘void Stack::pop()’:
prog.cpp:55:27: error: ‘size’ was not declared in this scope
   int* copy_arr = new int[size - 1];
                           ^~~~
prog.cpp:55:27: note: suggested alternative: ‘setSize’
   int* copy_arr = new int[size - 1];
                           ^~~~
                           setSize
prog.cpp:56:8: error: ‘size_t’ was not declared in this scope
   for (size_t i = 0; i < size - 1; i++) {
        ^~~~~~
prog.cpp:56:8: note: ‘size_t’ is defined in header ‘<cstddef>’; did you forget to ‘#include <cstddef>’?
prog.cpp:56:22: error: ‘i’ was not declared in this scope
   for (size_t i = 0; i < size - 1; i++) {
                      ^
prog.cpp: In member function ‘void Stack::swap(Stack&)’:
prog.cpp:66:3: error: ‘size_t’ was not declared in this scope
   size_t tmpSz = other.size;
   ^~~~~~
prog.cpp:66:3: note: ‘size_t’ is defined in header ‘<cstddef>’; did you forget to ‘#include <cstddef>’?
prog.cpp:67:9: error: ‘class Stack’ has no member named ‘size’; did you mean ‘setSize’?
   other.size = size;
         ^~~~
         setSize
prog.cpp:67:16: error: ‘size’ was not declared in this scope
   other.size = size;
                ^~~~
prog.cpp:67:16: note: suggested alternative: ‘setSize’
   other.size = size;
                ^~~~
                setSize
prog.cpp:68:10: error: ‘tmpSz’ was not declared in this scope
   size = tmpSz;
          ^~~~~
prog.cpp: In member function ‘int Stack::top()’:
prog.cpp:74:15: error: ‘size’ was not declared in this scope
   return data[size-1];
               ^~~~
prog.cpp:74:15: note: suggested alternative: ‘setSize’
   return data[size-1];
               ^~~~
               setSize
prog.cpp: In member function ‘bool Stack::isEmpty()’:
prog.cpp:77:10: error: ‘size’ was not declared in this scope
   return size == 0;
          ^~~~
prog.cpp:77:10: note: suggested alternative: ‘setSize’
   return size == 0;
          ^~~~
          setSize
prog.cpp: In member function ‘int Stack::getSize()’:
prog.cpp:83:10: error: ‘size’ was not declared in this scope
   return size;
          ^~~~
prog.cpp:83:10: note: suggested alternative: ‘setSize’
   return size;
          ^~~~
          setSize
prog.cpp: In function ‘void print(Stack&)’:
prog.cpp:97:7: error: ‘size_t’ was not declared in this scope
  for (size_t i = 0; i < stack.getSize(); i++) {
       ^~~~~~
prog.cpp:97:7: note: ‘size_t’ is defined in header ‘<cstddef>’; did you forget to ‘#include <cstddef>’?
prog.cpp:97:21: error: ‘i’ was not declared in this scope
  for (size_t i = 0; i < stack.getSize(); i++) {
                     ^
prog.cpp:98:8: error: ‘cout’ is not a member of ‘std’
   std::cout.width(7);
        ^~~~
prog.cpp:98:8: note: ‘std::cout’ is defined in header ‘<iostream>’; did you forget to ‘#include <iostream>’?
prog.cpp:1:1:
+#include <iostream>
 class Stack {
prog.cpp:98:8:
   std::cout.width(7);
        ^~~~
prog.cpp:99:8: error: ‘cout’ is not a member of ‘std’
   std::cout << stack.get(i);
        ^~~~
prog.cpp:99:8: note: ‘std::cout’ is defined in header ‘<iostream>’; did you forget to ‘#include <iostream>’?
prog.cpp:101:7: error: ‘cout’ is not a member of ‘std’
  std::cout << std::endl;
       ^~~~
prog.cpp:101:7: note: ‘std::cout’ is defined in header ‘<iostream>’; did you forget to ‘#include <iostream>’?
prog.cpp:101:20: error: ‘endl’ is not a member of ‘std’
  std::cout << std::endl;
                    ^~~~
prog.cpp:101:20: note: ‘std::endl’ is defined in header ‘<ostream>’; did you forget to ‘#include <ostream>’?
prog.cpp:1:1:
+#include <ostream>
 class Stack {
prog.cpp:101:20:
  std::cout << std::endl;
                    ^~~~
prog.cpp: In function ‘int main()’:
prog.cpp:114:7: error: ‘cout’ is not a member of ‘std’
  std::cout.width(7);
       ^~~~
prog.cpp:114:7: note: ‘std::cout’ is defined in header ‘<iostream>’; did you forget to ‘#include <iostream>’?
prog.cpp:115:7: error: ‘cout’ is not a member of ‘std’
  std::cout << stack.top();
       ^~~~
prog.cpp:115:7: note: ‘std::cout’ is defined in header ‘<iostream>’; did you forget to ‘#include <iostream>’?
stdout
Standard output is empty