Created
May 2, 2025 01:33
-
-
Save beginner/02dc194a5b497cff53175d3baf794d87 to your computer and use it in GitHub Desktop.
sec-20-video-242-template-class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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