Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save beginner/02dc194a5b497cff53175d3baf794d87 to your computer and use it in GitHub Desktop.
Save beginner/02dc194a5b497cff53175d3baf794d87 to your computer and use it in GitHub Desktop.
sec-20-video-242-template-class
template <int N>
class Array {
int size {N};
int values {N};
friend std::ostream &operator<<(std::ostream &os, const Array &arr) {
os << "[";
for (const auto &val: arr.values) {
os << val << " ";
}
os << "]";
os << std::endl;
return os;
}
public:
Array() = default;
Array(int init_val) {
for (auto &item: values) {
item = init_val;
}
}
void fill(int val) {
for (auto &item: values) {
item = val;
}
}
int get_size() const {return size;}
int &operator[](int index) {
return values[index];
}
};
// Error ->
// error: invalid range expression of type 'int'; no viable 'begin' function available
// 85 | for (auto &item: values) {
//error: invalid range expression of type 'const int'; no viable 'begin' function available
// 69 | for (const auto &val: arr.values) {
// | ^ ~~~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment