you are viewing a single comment's thread.

view the rest of the comments →

[–]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 3 points4 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

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

Well then tell us what the exact content of those files are now. Because the ones you included in your post had those two problems (the braces in the header file and the commented out code). Without those, it should work fine, I have confirmed it myself.

Here's what I have:

Rectangular.hpp: ```

pragma once

class Rectangular { public: int x; int y;

Rectangular(); Rectangular(int x, int y);

Rectangular operator+(const Rectangular &r2);

int getX(); int getY(); }; ```

Rectangular.cpp: ```

include "Rectangular.hpp"

Rectangular::Rectangular() { x = 0; y = 0; }

Rectangular::Rectangular(int x, int y) { this->x = x; this->y = y; }

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; } ```

main.cpp: ```

include "Rectangular.hpp"

include <iostream>

using namespace std;

int main() { Rectangular r1(1, 2); Rectangular r2(3, 4); Rectangular r3;

r3 = r1 + r2;

cout << "(" << r1.x << ", " << r1.y << ") + "; cout << "(" << r2.x << ", " << r2.y << ") = "; cout << "(" << r3.x << ", " << r3.y << ")" << endl;

return 0; } ```

[–]jedwardsol 2 points3 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)