vendredi 4 janvier 2019

Vector Initializer_list

I am trying to implement a vector using my own class so I can not do the initialiser list part

# include <iostream>
# include <exception>
# include<initializer_list>
template <class T>
 class vector {
  public:
  T* a;
T n;
int pos, c;
vector() { a = 0; n = 0; }
vector(T n) : n(n) { a = new T[n]; }
vector(std::initializer_list <vector> l) {
    a = new T[int(sizeof(l))];
        for (int f : l)
            *(a + f) = l.begin() + f;
     }
 void push_back(T k) {
    int i = k;
    *(a + n) = k;
}
 vector& operator= (vector&& th) {
     this->a = th.a;
    th.a = nullptr; return (*this);
}
vector& operator=(vector& k)
{
    this->a = k.a;
    return(*this);
}
int  size() { return n; }
void pop_back() { *(a + n) = nullptr;
n--;
}
void resize(int c) {  
    delete a;
    n = c;
 a = new T[c];
 }
 T operator[](int pos) {
    if (pos > sizeof(a))
        std::cout << "out of range";

    else return *(a + pos);
 }
 };
 int main() {
vector<int> a(10);
vector<char>b{ 'w','b','f','g' };
getchar();
return 0;

}

I am just trying to use pointer offset notation to take the initializer list items into the dynamic array but I get errors VS 17 IDE

Severity    Code    Description Project File    Line    Suppression   
Error   C2440   '=': cannot convert from 'const _Elem *' to 'T' 
Error   C2440   'initializing': cannot convert from 'const _Elem' to 'int'

Aucun commentaire:

Enregistrer un commentaire