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.

Recommended Answers

All 2 Replies

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;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.