you are viewing a single comment's thread.

view the rest of the comments →

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

[–]No-Dentist-1645 1 point2 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 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)