I programmed this vector class to support different types and different amount of dimensions. (class T,int D) D is the dimension.
I don't use templates much and I'm running into an error, my constructors aren't working.
Here is my class
#include <stdio.h>
#include <stdarg.h>
template <class T,int D>
class Vector
{
public:
Vector();
Vector(...);
Vector(const Vector<T,D>& vec);
~Vector();
const Vector<T,D>& operator=(Vector<T,D>);
Vector<T,D> operator+(Vector<T,D>);
Vector<T,D> operator-(Vector<T,D>);
Vector<T,D> operator*(Vector<T,D>);
Vector<T,D> operator/(Vector<T,D>);
const Vector<T,D>& operator+=(Vector<T,D>);
const Vector<T,D>& operator-=(Vector<T,D>);
const Vector<T,D>& operator*=(Vector<T,D>);
const Vector<T,D>& operator/=(Vector<T,D>);
bool operator==(Vector<T,D>);
//bool operator==(T);
//bool operator> (Vector<T,D>);
//bool operator< (Vector<T,D>);
//bool operator>=(Vector<T,D>);
//bool operator<=(Vector<T,D>);
//bool operator> (T);
//bool operator< (T);
//bool operator>=(T);
//bool operator<=(T);
T& operator[](const int& index);
//operator const T();
private:
//int Length();
T* dim;
};
template <class T,int D>
Vector<T,D>::Vector()
{
if(D > 0)
{
dim = new T[D];
for(int i = 0; i < D; i++)
dim[i] = 0;
}
}
template <class T,int D>
Vector<T,D>::Vector(...)
{
if(D > 0)
{
va_list dims;
va_start(dims,D);
dim = new T[D];
for(int i = 0; i < D; i++)
dim[i] = va_arg(dims,T);
va_end(dims);
}
}
template <class T,int D>
Vector<T,D>::Vector(const Vector<T,D>& vec)
{
if(D > 0)
{
dim = new T[D];
for(int i = 0; i < D; i++)
dim[i] = vec.dim[i];
}
}
template <class T,int D>
Vector<T,D>::~Vector()
{
if(D > 0)
delete [] dim;
}
template <class T,int D>
const Vector<T,D>& Vector<T,D>::operator=(Vector<T,D> v)
{
if(D > 0)
{
delete [] dim;
dim = new T[D];
for(int i = 0; i < D; i++)
dim[i] = v.dim[i];
}
return *this;
}
template <class T,int D>
Vector<T,D> Vector<T,D>::operator+(Vector<T,D> v)
{
Vector<T,D> _v;
for(int i = 0; i < D; i++)
_v.dim[i] = dim[i] + v.dim[i];
return _v;
}
template <class T,int D>
Vector<T,D> Vector<T,D>::operator-(Vector<T,D> v)
{
Vector<T,D> _v;
for(int i = 0; i < D; i++)
_v.dim[i] = dim[i] - v.dim[i];
return _v;
}
template <class T,int D>
Vector<T,D> Vector<T,D>::operator*(Vector<T,D> v)
{
Vector<T,D> _v;
for(int i = 0; i < D; i++)
_v.dim[i] = dim[i] * v.dim[i];
return _v;
}
template <class T,int D>
Vector<T,D> Vector<T,D>::operator/(Vector<T,D> v)
{
Vector<T,D> _v;
for(int i = 0; i < D; i++)
_v.dim[i] = dim[i] / v.dim[i];
return _v;
}
template <class T,int D>
const Vector<T,D>& Vector<T,D>::operator+=(Vector<T,D> v)
{
for(int i = 0; i < D; i++)
dim[i] += v.dim[i];
return *this;
}
template <class T,int D>
const Vector<T,D>& Vector<T,D>::operator-=(Vector<T,D> v)
{
for(int i = 0; i < D; i++)
dim[i] -= v.dim[i];
return *this;
}
template <class T,int D>
const Vector<T,D>& Vector<T,D>::operator*=(Vector<T,D> v)
{
for(int i = 0; i < D; i++)
dim[i] *= v.dim[i];
return *this;
}
template <class T,int D>
const Vector<T,D>& Vector<T,D>::operator/=(Vector<T,D> v)
{
for(int i = 0; i < D; i++)
dim[i] /= v.dim[i];
return *this;
}
template <class T,int D>
bool Vector<T,D>::operator==(Vector<T,D> v)
{
for(int i = 0; i < D; i++)
if(dim[i] != v.dim[i])
return false;
return true;
}
template <class T,int D>
T& Vector<T,D>::operator[](const int& index)
{
if(index >= 0 && index < D)
return dim[index];
return (T*)0;
}
typedef Vector<int,2> Vector2D;
typedef Vector<int,3> Vector3D;
typedef Vector<int,4> Vector4D;
And a basic initialization:
Vector2D v(1,2);
here are the errors i'm receiving:
\main.cpp||In function 'int main(int, char**)':|
\main.cpp|8|error: call of overloaded 'Vector()' is ambiguous|
\vector.h|59|note: candidates are: Vector<T, D>::Vector(...) [with T = int, int D = 2]|
\vector.h|49|note: Vector<T, D>::Vector() [with T = int, int D = 2]|
\main.cpp|8|warning: unused variable 'v'|
||=== Build finished: 3 errors, 1 warnings ===|
Thank you in advanced. Also if you see anything in my code that needs to be fixed feel free to tell me about them.