class Stack { public: Stack() = default; Stack(const Stack& other) :size(other.size) { this->data = new int[other.size]; for (size_t i = 0; i < other.size; i++) { this->data[i] = other.data[i]; } } Stack& operator=(const Stack& x) { if (&x != this) { delete[] data; this->data = new int[x.size]; for (size_t i = 0; i < x.size; i++) { this->data[i] = x.data[i]; } size = x.size; } return *this; } Stack(Stack&& other) noexcept : size(other.size), data(other.data) { other.data = nullptr; other.size = 0; } Stack& operator=(Stack&& x) noexcept { if (&x != this) { delete[] data; size = x.size; data = x.data; x.data = nullptr; x.size = 0; } return *this; } void setSize(size_t size) { this->size = size; } void push(int element) { int* copy_arr = new int[size + 1]; for (size_t i = 0; i < size; i++) { copy_arr[i + 1] = data[i]; } copy_arr[0] = element; size++; delete[] data; data = copy_arr; } void pop() { int* copy_arr = new int[size - 1]; for (size_t i = 0; i < size - 1; i++) { copy_arr[i] = data[i + 1]; } size--; delete[] data; data = copy_arr; } void swap(Stack& other) { if (this == &other) return; size_t tmpSz = other.size; other.size = size; size = tmpSz; int* tmpData = other.data; other.data = data; data = tmpData; } int top() { return data[size-1]; } bool isEmpty() { return size == 0; } int get(size_t index) { return data[index]; } int getSize() { return size; } ~Stack() { } private: size_t size; int* data = new int[size]; }; void print(Stack& stack) { for (size_t i = 0; i < stack.getSize(); i++) { std::cout.width(7); std::cout << stack.get(i); } std::cout << std::endl; } int main(){ Stack stack; stack.push(1); stack.push(2); stack.push(3); print(stack); stack.pop(); stack.pop(); print(stack); std::cout.width(7); std::cout << stack.top(); }
Standard input is empty
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>’?
Standard output is empty