dimanche 6 janvier 2019

Creating an object in C++ of a generic class with a constructor that uses an initializer_list

I have a homework assignment that requires me to create a generic class MyArray. I must have a constructor that uses an initializer_list. My code is:

    template<typename T>
    class MyArray{
      private:
        size_t n_{0};
        T* p_{nullptr};
      public:
      MyArray(std::initializer_list<T> a)  
          : n_{a.size}, p_{new T[n_]} {
            std::copy(begin(a),end(a),p_);
          }
     // destructor and other methods
    };

The problem is that when I try to create an instance of type MyArray e.g.

MyArray<int> arr{1,2,3,4};

I get an error that says

in instantiation of member function 'MyArray< int >::MyArray' requested here

I've just started learning generic classes so I really don't what I'm supposed to do here.

Aucun commentaire:

Enregistrer un commentaire