I have a dynamic array and i am trying to create a iterator class to support the list. I am adding begin and end. As well as a new class.
error C2146: syntax error : missing ';' before identifier 'begin'
Error 7 error C2146: syntax error : missing ';' before identifier 'end'
error C2244: 'vector::my_vector_iterator<DT>::my_vector_iterator' : unable to match function definition to an existing declaration
error C2653: 'my_vector_iterator' : is not a class or namespace name
error C2904: 'my_vector_iterator' : name already used for a template in the current scope
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
MAIN
int main()
{
return 0;
}
MY_VECTOR.h
//my_vector.h
#include "my_vector_iterator.h"
#include "my_vector_iterator.cpp"
#ifndef MY_VECTOR_H
#define MY_VECTOR_H
namespace vector
{
template<class T>
class my_vector
{
public:
//destructor
~my_vector();
//copy constructor
my_vector(const my_vector<T>& b);
//constructor that create an empty vector
my_vector();
/*constructor that create an vector with capacity specified
as a parameter but doesn't assign values to each element
in the vector*/
my_vector(unsigned int intial_Size);
/*constructor that create a vector with capacity specified
as a parameter and assign all the elements the value
specified*/
my_vector(unsigned int intial_Size, T assign);
//overloaded assignement operator
my_vector& operator=(const my_vector<T>& a);
//returns total number of elements in an array
int capacity();
//returns number of elements in array that are filled
int size();
//adds an value at next position in vector
void push_back(T user_number);
//prints out the vector that contains the numbers recursively
void show_vector(unsigned int siz);
//deletes the number from last element that stores a number
void pop_back();
//return value at the index specified by user
T at(size_t arrayindex);
//return value at index specified by user
T operator[](size_t array_index);
//return a iterator to first value in container
my_vector_iterator begin();
//return a iterator to value after one in container
my_vector_iterator end();
private:
//array to store numbers
T* numbers;
//total amount of elements that can be stored in array
int capacit;
//number of elements that are filled
int siz;
};
}
#endif
//Implementation of functions in my_vector.h
#include "my_vector.h"
#include <iostream>
using namespace std;
namespace vector
{
template<class T>
my_vector<T>::~my_vector()
{
delete[] numbers;
}
template<class T>
my_vector<T>::my_vector(const my_vector<T>& b)
{
capacit=b.capacit;
numbers=new T[capacit];
siz=b.siz;
for(long j=0; j<siz; j++)
{
numbers[j]=b.numbers[j];
}
}
template<class T>
my_vector<T>& my_vector<T>::operator=(const my_vector<T>& a)
{
capacit=a.capacit;
siz=a.siz;
if(this!=&a)
{
T* temp;
temp=a.numbers;
delete[] numbers;
numbers=new T[capacit];
for(long g=0; g<siz; g++)
{
numbers[g]=temp[g];
}
}
return *this;
}
template<class T>
my_vector<T>::my_vector()
{
siz=0;
capacit=0;
numbers=NULL;
}
template<class T>
my_vector<T>::my_vector(unsigned int intial_Size)
{
siz=0;
capacit=intial_Size;
numbers=new T[capacit];
}
template<class T>
my_vector<T>::my_vector(unsigned int intial_Size,T assign)
{
siz=0;
capacit=intial_Size;
numbers=new T[capacit];
for(long b=0; b<capacit; b++)
{
numbers[b]=assign;
siz++;
}
}
template<class T>
T my_vector<T>::at(size_t arrayindex)
{
return numbers[arrayindex];
}
template<class T>
T my_vector<T>::operator [](size_t arra)
{
return numbers[arra];
}
template<class T>
void my_vector<T>::push_back(T user_number)
{
T* temp;
//empty vector
if(capacit==0)
{
capacit=capacit+1;
numbers=new T[capacit];
numbers[0]=user_number;
siz++;
}
//last element doesn't contain a value
else if(siz!=capacit)
{
numbers[siz]=user_number;
siz++;
}
//last element contain a value so must expand the array
else if(siz==capacit)
{
temp=numbers;
numbers=NULL;
delete[] numbers;
numbers=NULL;
capacit=capacit*2;
numbers=new T[capacit];
for(long a=0; a<siz; a++)
{
numbers[a]=temp[a];
}
delete[] temp;
numbers[siz]=user_number;
siz++;
}
}
template<class T>
int my_vector<T>::capacity()
{
return capacit;
}
template<class T>
int my_vector<T>::size()
{
return siz;
}
template<class T>
void my_vector<T>::pop_back()
{
siz--;
}
template<class T>
void my_vector<T>::show_vector(unsigned int siz)
{
if(siz!=1)
{
show_vector(siz-1);
}
cout<<numbers[siz-1]<<"\n";
}
}
MY_ITERATOR.h
#ifndef MY_VECTOR_ITERATOR_H
#define MY_VECTOR_ITERATOR_H
#include "my_vector.h"
//#include "my_vector.cpp"
namespace vector
{
template<class DT>
class my_vector_iterator
{
public:
//default constructor
my_vector_iterator();
//increment the pointer
void operator++();
//return value by derefencing
my_vector<DT> operator*(my_vector& z);
//
bool operator!=(my_vector_iterator<DT>& b);
private:
//used to say current position
int cursor;
};
}
#endif;
MY_ITERATOR.cpp
#include "my_vector_iterator.h"
namespace vector
{
template<class DT>
my_vector_iterator::my_vector_iterator()
{
cursor=0;
}
}