InvI
InvI

Reputation: 81

+ operator overload to add different C++ class object

I have seen many example to add objects of same class.I was trying to add two different class objects using operator overloading. Code:

#include<iostream>
using namespace std;
class B;
class A
{
public:
int x;
A(int t=99)
{
x=t;
}
friend const A operator+( A& m, B& n);
friend ostream& operator<<(ostream& os, const A& c);
};

const A operator+(A& c1,B& c2) 
{
     A temp;
     temp.x = c1.x + c2.y; 
         return temp;
 }
 ostream& operator<<(ostream &os, const A& c)
 { 
    os << c.x;
    return os;
}


class B
{
public:
int y;
B(int e=90)
{
y=e;
}
friend const A operator+( A& m, B& n);
};

int main()
{
A a,u;
B b;
u=a+b;
cout<<"Value of A+B"<<u;
return 0;
}   

When i compiled my code it shows Error:

$ g++ operator_overloading.cpp

operator_overloading.cpp: In function ‘const A operator+(A&, B&)’:

operator_overloading.cpp:19:21: error: invalid use of incomplete type ‘struct B’

operator_overloading.cpp:3:7: error: forward declaration of ‘struct B’

What i have done wrong??

Upvotes: 0

Views: 5761

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

You have to define the operator after the definition of class B. For example

#include<iostream>
using namespace std;

class B;
class A
{
public:
int x;
A(int t=99)
{
x=t;
}
friend const A operator+( const A& m, const B& n);
friend ostream& operator<<(ostream& os, const A& c);
};

 ostream& operator<<(ostream &os, const A& c)
 { 
    os << c.x;
    return os;
}


class B
{
public:
int y;
B(int e=90)
{
y=e;
}
friend const A operator+( const A& m, const B& n);
};

const A operator+(const A& c1, const B& c2) 
{
     A temp;
     temp.x = c1.x + c2.y; 
         return temp;
 }

 //...

Otherwise the compiler does not know what data members class B has. Also it is better to define paraneters of the operator as constant references. In this case the operator can deal with temporary objects.

Upvotes: 1

Christian Hackl
Christian Hackl

Reputation: 27528

The line class B; forward-declares B. This tells the compiler that a class called "B" exists, but nothing else. When you attempt to use c2.y, where c2 is a B, the compiler does not yet know that B even has a y member.

One solution in this case is to move the definition of B so that it appears before the operator+ definition:

class B;
class A
{
public:
int x;
A(int t=99)
{
x=t;
}
friend const A operator+( A& m, B& n);
friend ostream& operator<<(ostream& os, const A& c);
};

class B
{
public:
int y;
B(int e=90)
{
y=e;
}
friend const A operator+( A& m, B& n);
};

const A operator+(A& c1,B& c2) 
{
     A temp;
     temp.x = c1.x + c2.y; 
         return temp;
 }

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385104

The error is clear. You have attempted to use members of B using only a forward declaration.

Upvotes: 1

Christophe
Christophe

Reputation: 73366

The error message is caused by the fact that you've defined your const A operator+(A& c1,B& c2) before defining the class B.

At this moment B is hence still an incomplete type (meaning you can only use pointers and references to it, but nothing else).

Just move this definition after you've defined B.

Upvotes: 0

Related Questions