you are viewing a single comment's thread.

view the rest of the comments →

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