hi
i have a problem in this code:
#include<iostream.h>
class exforsys{
private:
int x,y;
public:
exforsys(int x,int y){x=0; y=0;}
void getvalue(){
cout<<"\n enter value for x:";
cin>>x;
cout<<"\n enter value for y:";
cin>>y;
}
void displayvalue(){
cout<<"value of x is:"<<"value of y is:"<<y; }
exforsys operator + (exforsys);
};
exforsys exforsys :: operator +(exforsys e2){
int x1=x+e2.x;
int y1=y+e2.y;
return exforsys(x1,y1);
}
void main(){
exforsys e1,e2,e3;
cout<<"enter value for object e1:";
e1.getvalue();
cout<<"enter value for obgect e2:";
e2.getvalue();
e3=e2+e1;
}
"could not find a match for exforsys::exforsys()" compiler says for the line after where the main function starts.what is my fault!?
The error that you posted is saying that the compiler doesn't like the following line because there is no default constructor. The compiler-generated default constructor is no longer available once you define a different constructor.
exforsys e1,e2,e3;
Consider adding one and repairing the current constructor that blanks out x and y.
You'll also want to define a copy constructor, so that the parameter passed by value to operator+ works. The parameter should probably be const exforsys & e2, rather, and in that case I think you'll still need the copy constructor to return by value. There is a return value optimization that may remove the copy, so I'm not 100% certain that it would be required.
Oh! Your last line would also require an assignment operator.
When you declare a non-default constructor, the compiler does not give you a default constructor any more; you
have to explicitly declare and implement it. The line in main that the compiler is complaining about is attempting
to default construct three instances of exforsys.