'I was given to implement this function: name operator + (name const &) using operator+() in below program
#include<iostream>
using namespace std;
class name
{
int x,y;
public:
name(int a, int b) { x=a; y=b; }
name operator + (name const &obj){
name one;
one.x = x + obj.x;
one.y = y + obj.y;
return res;
}
void print(){
cout<<"Values are: "<<x<<","<<y<<endl;}
};
int main()
{
name c1(4,4),c2(6,6);
name c3=c1+c2;
c3.print();
return 0;
}
This displays error: [Error] no matching function for call to 'name::name()'
Could anyone tell me what is wrong with the code?
Solution 1:[1]
You are missing the default constructor, thus one of the working solution might be
name operator + (name const &obj){
name res(x + obj.x, y + obj.y);
return res;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 273K |
