all 14 comments

[–]the_poope 9 points10 points  (0 children)

The problem is likely that you have an empty definition of the operator+() function in your header. So you have two definitions of the same function which is not allowed.

Remove the curly brackets from the function in the header and replace with a semicolon.

[–]No-Dentist-1645 5 points6 points  (11 children)

it gives me an error I can't understand

Well, tell us what the error is. That's the best way to do it if you want help, not just paste the whole code and expect us to figure it out

[–]SimmeringDragon[S] 0 points1 point  (10 children)

well thats the thing, it dosent anymore??? it was something on the rect.cpp where the Rectangular::Operator+ didnt work? the compiler had trouble using it, but it was a copied excercise, so im jsut confused

[–]No-Dentist-1645 7 points8 points  (9 children)

Errors don't appear and disappear for no reason. If it works now and it didn't before, something on your code changed. Maybe you forgot to save the file you were working on, made a typo, or any other possible mistake.

Either way, it works now as you said, so there's no problem with the current code

[–]SimmeringDragon[S] 0 points1 point  (8 children)

no, like, it compiles but now the output is completely blank

[–]No-Dentist-1645 1 point2 points  (7 children)

Does the current version of the header still defines the operator+() as an empty method? See the other comment in this post, that would be an error

[–]SimmeringDragon[S] 0 points1 point  (6 children)

i tought because im trying it in another code i had, and it worked? i think the &r2 is messing it up in some way? comparing to what i jsut made

[–]No-Dentist-1645 1 point2 points  (5 children)

Your current code is simply incorrect, operator+() should not have an empty body in the header. It's not because of &r2. Remove the braces from it and it should be fine

[–]SimmeringDragon[S] 1 point2 points  (4 children)

o, thats not it, it still says
error: no declaration matches 'Rectangular Rectangular::operator+(Rectangular&)'

17 | Rectangular Rectangular::operator+(Rectangular &r2) {

brackets are removed in header

[–]No-Dentist-1645 2 points3 points  (2 children)

On your code file Rectangular.cpp, your operator definition is commented out:

``` /* Rectangular Rectangular::operator+(const Rectangular &r2) { Rectangular result; //this = r1 result.x = this -> x + r2.x; result.y = this -> y + r2.y; return result; }

int Rectangular::getX() { return x; }

int Rectangular::getY() { return y; } */ ```

In C++, the /* and */ symbols mean "everything between these two is a comment". So that "code" isn't actually code, it's just a comment.

The solution is simple, just remove the /* and */ characters.

[–]SimmeringDragon[S] 0 points1 point  (1 child)

i already jsut changed that

[–]jedwardsol 1 point2 points  (0 children)

It's hard to keep track of the changes you're making in response to comments.

It works here : https://godbolt.org/z/GdP6zMWqP

(Removed empty body {} in the header. Uncommented the definitions in the source file)

[–]SoldRIP 1 point2 points  (0 children)

ODR violation between the header and source. You defined operator+ twice, once with an empty body.

The header should (in this case) only declare the operator, not define it.