I was given to implement this function: name operator + (name const &) using operator+() in below program

Viewed 56
#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?

1 Answers

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;
}
Related