Hi -i've started using netbeans and compiling in it recently and I'm not sure what the problem is here. Not sure if it isn't reading the Element.cpp/.h files while building/running the main.cpp

In my main:

Element a, b(5);

cout<<(5+b)<< endl;

Then in the .h:

Element operator+(const Element&obj);

And in the element.cpp:

 Element Element::operator+(const Element&obj){
     return Element(defaultArgument + obj.defaultArgument);

defaultArgument is a double variable just so btw!

The error states: error: no match for 'operator+' in '5 + b' - maybe just something small. Bit rusty with c++ atm.

Edited 4 Years Ago by Phillamon

Your operator overload is for Element + Element not for int + Element or Element + int.

You can fix the problem by defining those operator overloads, as free-functions:

Element operator+(const Element& lhs, int rhs);
Element operator+(int lhs, const Element& rhs);

and implementing them as you see fit.

BTW, it is also preferred to implement all operators as free-functions (or friend-functions), not as member functions. There are technical reasons for that.

Several things. This function (which should be a const function as in Element operator+(const Element&obj) const;) adds one Element to another. That isn't what you are doing. It may work if you have an Element constructor that takes a single integer argument, but in your case, you should create an Element operator+(int) const method, and then reverse the order of arguments in your output stream to cout << (b+5) << endl;

Edited 4 Years Ago by rubberman

This question has already been answered. Start a new discussion instead.