Overloading on templates
Jun 12, 2009 at 10:35am UTC
Hello, I am trying to overload == but I always get the error
error C2679: binary '==' : no operator found which takes a right-hand operand of type 'Vector<T>' (or there is no acceptable conversion)
Here is the overload
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
template <class T>
bool Vector<T>::operator ==(Vector<T> vec)
{
bool v=0;
if (size!=vec.size)
return v;
else
{
v=1;
for (int i=0;i<size;i++)
if (storage[i]!=vec.storage[i])
v=v && 0;
return v;
}
Do you have any idea what the issue is?
Thanks
Jun 12, 2009 at 11:57am UTC
The right declaration is
1 2 3 4 5
template < typename T >
bool Vector<T>::operator ==( const Vector<T>& rhs ) const
{
// ...
}
EDIT:
and
bool v = false; // not 0
etc.
Last edited on Jun 12, 2009 at 12:00pm UTC
Jun 12, 2009 at 12:19pm UTC
Thanks, I made the proper corrections, but I still get the same error
Jun 12, 2009 at 1:33pm UTC
could u attach the whole class to be able to serve you.
I think the solution of :
error C2679: binary '==' : no operator found which takes a right-hand operand of type 'Vector<T>' (or there is no acceptable conversion)
as jsmith said .
Last edited on Jun 12, 2009 at 1:35pm UTC
Jun 12, 2009 at 1:54pm UTC
Here it is, thanks for the help. I just figured that operator + is not working as well
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include<iostream>
using namespace std;
template <class T>
class Vector
{
T* storage;
int size;
public :
Vector();
Vector(int a);
Vector(const Vector<T>& vec);
Vector operator -();
Vector operator +(Vector<T>& vec);
bool operator ==(const Vector<T>& vec)const ;
T operator [](int );
//-----------------------ostream operator overload-------------------------------------------
friend ostream& operator <<(ostream& output,Vector<T>& vec)
{
output<<"(" ;
for (int i=0;i<vec.size-1;i++)
output<<vec.storage[i]<<"," ;
output<<vec.storage[vec.size-1];
output<<")" <<endl;
return output;
}
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#include "Vector.h"
#include<iostream>
using namespace std;
template <class T>
Vector<T>::Vector()
{
size=1;
storage=new T[size];
}
template <class T>
Vector<T>::Vector(int a)
{
size=a;
storage= new T[size];
cout<<"Enter vector elements" <<endl;
for (int i=0;i<size;i++)
cin>>storage[i];
}
//--------------------copy constructor------------------------------------------------------
template <class T>
Vector<T>::Vector(const Vector<T>& vec)
{
cout<<"copy ctor" <<endl;
storage=new T[vec.size];
for (int i=0;i<size;i++)
storage[i]=vec.storage[i];
}
//-------------------------------operator (-)------------------------------------------------
template <class T>
Vector<T> Vector<T>::operator -()
{
for (int i=0;i<size;i++)
storage[i]=-storage[i];
return *this ;
}
//------------------------------operator (+)-------------------------------------------------
template <class T>
Vector<T> Vector<T>::operator +(Vector<T>& vec)
{
if (size>=vec.size)
{
Vector<T>tmp[size];
for (int i=0;i<size;i++)
tmp.storage[i]=storage[i]+vec.storage[i];
return <T>tmp;
}//end if
else
{
Vector<T> tmp[vec.size];
for (int i=0;i<size;i++)
tmp.storage[i]=storage[i]+vec.storage[i];
return tmp;
}
}
//-----------------------------------------operator == -------------------------------
template <class T>
bool Vector<T>::operator ==(const Vector<T>& vec)const
{
bool v=false ;
if (size!=vec.size)
return v;
else
{
v=true ;
for (int i=0;i<size;i++)
if (storage[i]!=vec.storage[i])
v=v && true ;
return v;
}
}
//---------------------------------operator [] -----------------------------------
template <class T>
T Vector<T>:: operator [](int n)
{
return storage[n];
}
int main()
{
Vector<int >a(2);
Vector<int >b(1);
cout<<b==a;
b=b+a;
}
Jun 12, 2009 at 2:03pm UTC
Line 103:
cout << ( b == a );
Jun 12, 2009 at 2:04pm UTC
Line 80:
return false;
Jun 12, 2009 at 2:06pm UTC
Line 25:
friend ostream& operator<<(ostream& output, const Vector<T>& vec);
operator+ is totally wrong.
operator+ should also take parameter by const reference.
Jun 12, 2009 at 2:09pm UTC
One could also argue that since operator[] is returning by value, it should be a const member function.
Typically one does:
1 2
template < typename T > T Vector<T>::operator []( int n ) const { return storage[n]; }
template < typename T > T& Vector<T>::operator []( int n ) { return storage[n]; }
second form allows the expression:
vec[ 5 ] = 4;
assuming vec is of type Vector<T> where T is int or any other type implicitly convertible to int.
Jun 12, 2009 at 2:20pm UTC
Thanks, you really helped me, == operator is ok, I will start to work on the + operator, I appreciate your help.
Topic archived. No new replies allowed.