Upon request I have post the rest of the code in idone.com
So I have to create my own Vector Class and I did. It is required.
When I implement it in another class however, It gives me these errors
This is how I am trying to implement it:
#include "Vector.h"
class UserDB
{
private:
Vector _accounts;
//more code that previous to this implementation worked fine.
};
I was hoping someone could tell me what to do about it. I'm completely clueless in this one. Java never gave such errors, whatsoever...
EDIT: I have also realized that when I say something like Vector
it runs completely fine, however when done with a class such as Vector, it seems almost impossible to fix it. I am reading a lot, but I still cannot seem to find a fix to this.
This is my Vector Header
#include "ArrayClass.h" //Class with more methods
#include "AbstractVector.h" //Another class with more method
template
class Vector: virtual public ArrayClass,
virtual public AbstractVector
{
protected:
int _currSize;
int _incFactor;
public:
Vector ();
Vector (int n);
Vector (int n, DT& val);
Vector (const Vector& v);
Vector (const ArrayClass& ac);
virtual ~Vector();
void operator= (const Vector& v);
void operator= (const ArrayClass& ac);
virtual void insert (const DT& item, int index);
virtual void remove (int index);
virtual void add (const DT& item);
virtual int size() const;
virtual int capacity() const;
virtual int incFactor() const;
virtual void setIncFactor(int f);
void setCapacity(int c);
};
And this one is the Actual code Vector.cpp
#include "Vector.h"
template
Vector::Vector () : ArrayClass()
{
_currSize = 0;
_incFactor = 5;
}
Vector::~Vector ()
{
_currSize = NULL;
_incFactor = NULL;
}
template
Vector::Vector (int n): ArrayClass(n)
{
_currSize = 0;
_incFactor = (n+1)/2;
}
template
Vector::Vector (int n, DT& val)
: ArrayClass(n, val)
{
_currSize = 0;
_incFactor = n/2;
}
template
Vector::Vector (const Vector&v)
: ArrayClass (v)
{
_currSize = v._currSize;
_incFactor = v.incFactor();
}
template
Vector::Vector (const ArrayClass& ac)
: ArrayClass (ac)
{
_currSize = ac.size();
_incFactor = (_currSize+1)/2;
}
template
void Vector::operator= (const Vector& v)
{
ArrayClass::copy (v);
_currSize = v._currSize;
_incFactor = v.incFactor();
}
//template
//void Vector::operator= (const ArrayClass&ac)
//{
// ArrayClass::copy (ac);
// _currSize = ac.size();
// _incFactor = (_currSize+1)/2;
//}
template
int Vector::size() const
{
return _currSize;
}
template
int Vector::capacity() const
{
return _size;
}
template
int Vector::incFactor() const
{
return _incFactor;
}
template
void Vector::add (const DT& item)
{
insert (item, _currSize);
}
template
void Vector::setIncFactor(int f)
{
if (f >= 0) _incFactor = f;
}
template
void Vector::setCapacity(int c)
{
int len = _currSize;
if (len > c) len = c;
DT* paNew = new DT[c];
if (paNew == NULL)
{
throw ArrayMemoryException();
}
for (int i = 0; i < len; i++)
{
paNew[i] = paObject[i];
}
if (paObject != NULL)
{
delete[] paObject;
}
paObject = paNew;
_size = c;
if (_currSize > len)
{
_currSize = len;
}
}
template
void Vector::insert (const DT& item, int index)
{
if ((index < 0) || (index > _currSize))
{
throw ArrayBoundsException();
}
if (_currSize+1 == _size)
{
setCapacity (_size + _incFactor);
}
_currSize++;
for (int i = _currSize-1; i > index; i--)
{
(*this)[i] = (*this)[i-1];
}
(*this)[index] = item;
}
template
void Vector::remove (int index)
{
if ((index < 0) || (index >= _currSize))
{
throw ArrayBoundsException();
}
if (_currSize <= _size-_incFactor)
{
setCapacity (_size - _incFactor);
}
for (int i = index; i < _currSize-1; i++)
{
(*this)[i] = (*this)[i+1];
}
_currSize--;
}
Answer
While compiling your code, at the point, where template is being instantiated, i.e. in your code
Vector _accounts;
the compiler will generate a new class using template Vector
( with given template argument i.e AccountInfo*
). Thus compiler needs access to implementation of template Vector
at this point.
So, this implies, the template Vector
should be completely implemented in a header file.
If you do want to keep the separation between template definition and implementation then
you can have two header files, one Vector.h
and other Vector_impl.h
, with Vector_impl.h
containing the implementation of the template.
Include Vector.h
in the Vector_impl.h
and Include Vector_impl.h
in the .cpp file where you are instantiating the template i.e. Vector < AccountInfo*>.
No comments:
Post a Comment