Hi
I have written a simple template class.
#include <iostream>
using namespace std;
template <class T>
class vector
{
T *arr;
int size;
public:
vector(int len)
{
arr = new T[size=len];
for(int i=0;i<len;i++)
arr[i]=0;
}
vector(T *list)
{
for(int i=0;i<size;i++)
arr[i]=list[i];
}
T operator *(vector &obj)
{
T sum=0;
for(int i=0;i<size;i++)
sum+=this->arr[i]*obj.arr[i];
return sum;
}
};
int main()
{
vector <int>v1(3);
vector <float>v2(3);
int arr1[]={1,2,3};
float arr2[]={1.1,2.2,3.3};
v1=arr1;
v2=arr2;
float prod = v1*v2;
//cout<<prod;
return 0;
}
But on compiling it C++ gives error as
error C2679: binary '*' : no operator found which takes a right-hand
operand of type 'vector<T>' (or there is no acceptable conversion)
with
[
T=float
]
could be 'int vector<T>::operator *(vector<T> &)'
with
[
T=int
]
while trying to match the argument list '(vector<T>,
vector<T>)'
with
[
T=int
]
and
[
T=float
]
Is it because compiler could not find a way to convert from either
float to int or vice versa or something else? Also what is the
solution of this
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.