I am trying to create my own vector class. Currently, im working on adding an erase function to my vector class called, Vec. My code complains when i pass variable.begin() into the erase function parameter but function properly after i manually converted variable.begin() into iterator then pass it in.
Can someone explain to me why this bizarre situation is occuring?
int main(){
Vec<string> words;
words.push_back("h");
words.push_back("a");
words.push_back("p");
Vec<string>::iterator iter=words.begin();
Vec<string>::iterator iter1=words.end();
words.erase(words.begin(),words.end());//this does not work
words.erase(iter,iter1);//this works
}
//Function erase in the Vec class i created
template <class T> class Vec {
typedef T* iterator;//
typedef const T* const_iterator;
iterator data; // first element in the Vec
iterator avail; // (one past) the last element in the Vec
iterator limit; // (one past) the allocated memory
void erase(iterator&i, iterator&j){
iterator new_limit=limit;
iterator new_avail=avail;
size_t a=j-i+1;
size_t n=limit-data;
n=n-a;
iterator new_data=alloc.allocate(n);
iterator mid=std::uninitialized_copy(data,data+1,new_data);
size_t b=size();
if(j!=avail){
new_limit, new_avail=std::uninitialized_copy(j+1,avail,mid);
data=new_data;
avail=new_avail;
limit=new_limit;
}
else{
data=new_data;
limit=avail=data+n;
}
}
}