Reputation: 18594
I'm trying to overload the + operator in C++, but get the following error:
operators.cpp: In function ‘int main()’:
operators.cpp: 23:17: error: cannot convert ‘Operators’ to ‘int’ in initialization
This is my code:
#include <iostream>
using namespace std;
class Operators{
private:
int num1;
public:
Operators(int num1){
this->num1 = num1;
}
Operators operator+(Operators o){
return Operators(num1 + o.num1);
}
};
int main(){
Operators o1(5);
Operators o2(10);
Operators res = o1 + o2; // EDITED
cout << res;
}
Could you please help me?
I know, in this case it doesn't make sense to overload it, as I could just say 5+10
, but I'm just experimenting.
UPDATE
Thanks, I've edited the int
.
But now I'm getting the following error:
operators.cpp: In function ‘int main()’:
operators.cpp: 25:10: error: match for ‘operator<<’ in ‘std::cout << res’
[...]
Upvotes: 0
Views: 152
Reputation: 126432
The problem with this line:
int res = o1 + o2;
Is that your overload of operator +
returns an object of type Operators
. This means that you are trying to initialize an int
(res)
from a temporary of type Operators
(the result of o1 + o2
), but there is no user-defined conversion for doing that.
This is why the compiler is issuing an error. You can fix this easily by adding a conversion operator to your Operators
class:
operator int () const { return num1; }
UPDATE:
It seems you have updated your question. The second line below is problematic:
Operators res = o1 + o2;
cout << res;
Unless you defined an overload of operator <<
for your Operators
class, the compiler won't know which overload of operator <<
to pick for streaming your object of type Operators
.
To solve the issue, you can:
int
, as suggested aboveDefine an overload of operator <<
for Operators
, as follows:
friend std::ostream& operator << (std::ostream& o, Operators const& op)
{
o << op.num1;
return o;
}
Here is a live example.
Upvotes: 4
Reputation: 96810
Change int
to Operators
, and implement your own copy and default constructor.
#include <iostream>
class Operators
{
private:
int num1;
public:
Operators() = default;
Operators(Operators const& op) : num1(op.num1) {}
Operators(int num1)
{
this->num1 = num1;
}
Operators operator+(Operators o)
{
return num1 + o.num1;
}
};
int main()
{
Operators o1(5);
Operators o2(10), res;
res = o1 + o2;
std::cout << res;
}
To make this work further, create an overload of operator<<
and print the sum.
Upvotes: 0
Reputation: 1
You need to write:
int res = o1.getMethod() + o2.getMethod();
where getMethod() is a public method in your class that returns the private integer num1. Otherwise, you don't have access to it. Try it.
Upvotes: 0
Reputation: 110658
Your operator+
returns an Operators
object. You then try and assign this result to int res
. YOu have given no way to convert from an Operators
object to an int
. You could provide a conversion operator to do this:
operator int() {
return num1;
}
This defines a conversion from Operators
to int
.
Upvotes: 1
Reputation: 887453
As the error message is trying to tell you, o1 + o2
is of type Operators
.
You can't assign that to an int
Upvotes: 3