Hi All ,
In below code i am getting error while assigning one object with other and result is clear as both objects are different types.
it is expected that RHS object should belong to same class. How to resolve this issue.
error: no match for operator= in obj1 = obj2;
error: candidates are: const b& b::operator=(const b&)
#include <iostream>
using namespace std;
class a
{
int a1 ,b;
public:
a(int x=0,int y=0):a1(x),b(y){}
void fun1() {cout<<a1;cout<<b;}
};
class b
{
private:
a *p;
public:
b (int x=0, int y=0)
{
p= new a(x,y);
}
b ( const b &obj)
{
p = new a (*obj.p);
}
const b& operator=(const b &obj)
{
if(this !=&obj)
{
delete p;
p=new a(*obj.p);
}
return obj;
}
void fun2(){p->fun1();}
~b () { delete p;}
};
int main()
{
b obj1(10,20);
a obj2(20,30);
obj1=obj2;//error
//obj1.fun2();
//b obj2(obj1);
//obj1.fun2();
return 1;
}