vendredi 22 mai 2020

Begin was not declared in the scope for Generic Array Template Class

#include <iostream>
#include <string>

template <typename T, int N>
class Array {
    int size{N};
    T values{N};

    friend std::ostream &operator<<(std::ostream &os, const Array<T, N> &arr) {
        os << "[";
        for (const auto &val: arr.values)
            os << val << " ";
        os << "]" << std::endl;
        return os;
    }
public:
    Array() = default;
    Array(T value_added) {
        for(auto &value: values)
            value = value_added;
    }

    void fill(T data_value) {
        for (auto &value: values)
            value = data_value;
    }

    int get_size() const {
        return size;
    }

    T &operator[](int index) {
        return values[index];
    }
};

int main () {
    Array<int,2> nums;
    std::cout << "The size of nums is: " << nums.get_size() << std::endl;
    std::cout << nums << std::endl;
}

I am trying to create a generic array template class but I get the error Begin was not declared in the scope and it says it is due to the for (const auto &val: arr.values) loop.

Aucun commentaire:

Enregistrer un commentaire