i have following class with overloading operators
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
class Cvector
{
public:
int x,y;
Cvector() { x=0;y=0;}
Cvector(int,int);
Cvector operator+(Cvector);
Cvector operator-(Cvector);
int operator*(Cvector);
bool operator==(Cvector);
Cvector operator*(int);
Cvector operator=(Cvector);
int cross_multiplication(Cvector,Cvector);
float norm();
};
Cvector Cvector::operator=(Cvector a)
{
x=a.x;
y=a.y;
return *this;
}
bool Cvector::operator==(Cvector b)
{
return (x==b.x && y==b.y);
}
Cvector Cvector::operator*(int c)
{
Cvector temp;
temp.x=c*x;
temp.y=c*y;
return temp;
}
float Cvector::norm()
{
float result=0;
result+=x*x+y*y;;
return sqrt(result);
}
Cvector::Cvector(int a,int b)
{
x=a;
y=b;
}
Cvector Cvector::operator+(Cvector a)
{
Cvector temp;
temp.x=x+a.x;
temp.y=y+a.y;
return temp;
}
Cvector Cvector::operator-(Cvector b)
{
Cvector temp;
temp.x=x-b.x;
temp.y=y-b.y;
return temp;
}
int Cvector::operator*(Cvector a)
{
return x*a.x+y*a.y;
}
int main()
{
Cvector a(3,4);
Cvector b(4,5);
cout<<b.norm()<<endl;
Cvector c;
c=a*b;
cout<<(a==b)<<endl;
return 0;
}
but it gives me one error
1>c:\users\dato\documents\visual studio 2010\projects\point_class\point_class\point_class.cpp(86): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
1> c:\users\dato\documents\visual studio 2010\projects\point_class\point_class\point_class.cpp(16): could be 'Cvector Cvector::operator =(Cvector)'
1> while trying to match the argument list '(Cvector, int)'
please help me to fix this problem
returns an
int, so:tries to assign an
intto aCvectorobject, which needs a overloaded=operator ofCvectorclass to takeintas parameter, which it doesn’t. Hence the error.chas no business being aCvector, it should be anintideally, so either it could be a typo or it could indeed be the intent(which does break conventional algebraic wisdon).I am not sure which one it is.If it is the former, just change type of
ctoint.If it is latter, You need to provide a constructor which takes in an
intparameter:This constructor will implicitly called to convert the
intresult ofoperator *and convert it to anCvectorobject which will then be used to call theoperator =.Online Demo of your code with suggested solution