Operator Overloaded My Brain

Found this practice exercise in a C++ book and haven't looked away from my computer in...let's not talk about it. Experiencing burn out. Take a look and help if you can. Be gentle...or don't. Either way.

Error is "system cannot find the file specified"

Suppose to be doing an Operator Overload

.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include "rectangleType.h"

using namespace std;

void rectangleType::setDimension(double l, double w)  



{
	if (l >= 0)
		length = 1;
	else
		length = 0;
	if (w >= 0)
		width = w;
	else
		width = 0;
}

double rectangleType::getLength() const			
{
	return length;
}

double rectangleType::getWidth() const
{
	return width;					
}

double rectangleType::area() const   
{
	return length * width;
}

double rectangleType::perimeter() const  
{
	return 2 * (length + width);
}

void rectangleType::print() const
{
	cout << "Length = " << length << ", Width = " << width;
}

rectangleType::rectangleType(double l, double w) {
	setDimension(l, w);
}

rectangleType::rectangleType()						
{
	length = 0;
	width = 0; 
}

rectangleType rectangleType::operator +(const rectangleType& rect) const  
{
	rectangleType temp;		 
	temp.length = length + rect.length;
	temp.width = width + rect.width;
	return temp;
}

rectangleType rectangleType::operator *(const rectangleType& rect) const  
{
	rectangleType temp;														 
	temp.length = length * rect.length;
	temp.width = width * rect.width;
}

bool rectangleType::operator ==(const rectangleType& rect) const       //relational operator
{
	return (length == rect.length && width == rect.width);					
}

bool rectangleType::operator !=(const rectangleType& rect) const
{
	return (length != rect.length || width != rect.width);
}

ostream& operator<< (ostream& out, const rectangleType& rect)                
{
	out << "Length = " << rect.getLength << ", Width = " << rect.getWidth;
	return out;
}

istream& operator>> (istream& in, rectangleType& rect)
{
	in >> rect.length >> rect.width;
	return in;
}



.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#pragma once
#ifndef H_rectangleType
#define H_rectangleType
#include <iostream>

class rectangleType
{
	friend ostream& operator<< (ostream&, const rectangleType&);
	friend istream& operator>> (istream&, rectangleType&);
	 
public:                                     
	void setDimension(double,double);
	double getLength() const;
	double getWidth() const;
	double area() const;
	double perimeter() const;
	void print() const;
	rectangleType();			
	rectangleType(double, double); 

	rectangleType operator+ (const rectangleType&) const;
	rectangleType operator* (const rectangleType&) const;
	bool operator==(const rectangleType&) const;					
	bool operator!=(const rectangleType&) const;

private: 
	double length;
	double width;
};

#endif 


main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "rectangleType.h"

using namespace std;

int main()
{
	rectangleType rect1(23,45), rect2(12,10), rect3, rect4; 
	cout << "rect1 = "; 
	rect1.print();
	cout << endl << "rect2 = ";
	rect2.print();
	cout << endl;
	rect3 = rect1 + rect2;					
	cout << "rect3 = ";
	rect3.print();
	cout << endl;
	rect4 = rect1 * rect2;					
	cout << "rect4 = ";
	rect4.print();
	cout << endl;

	if (rect1 == rect2)
		cout << "rect 1 and rect2 are equal." << endl;
	else
		cout << "rect1 and rect2 are not equal." << endl;
	if (rect1 != rect3)
		cout << "rect1 and rect3 are not equal." << endl;
	else
		cout << "rect1 and rect3 are equal." << endl;

	system("PAUSE");
	
	return 0;

}
Last edited on
if you literally copy and pasted this from the book you're reading and it gives you an error perhaps it is not a good book.
Last edited on
*facepalm* Maybe I was unclear due to having jello for brains...

The problem was about operator overloading, in the book I'm reading with some clips and instructions, but you have to add the rest in yourself and solve the issue. Which I have attempted and failed.

The books says:

"Redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts a to c."

"Overload the pre- and post-increment and decrement operators to increment and decrement, respectively, the length and width of a rectangle by one unit.

Overload the binary operator – to subtract the dimensions of one rectangle from the corresponding dimensions of another rectangle.

The operators == and != are overloaded by considering the lengths and widths of rectangles. Redefine the functions to overload the relational operator by considering the areas of rectangles as follows: Two rectangles are the same if they have the same area; otherwise, the rectangles are not the same.

Similarly, rectangle yard1 is greater than rectangle yard2 if the area of yard1 is greater than the area of yard2. Overload the remaining relational operators using similar definitions.

Write the definitions of the functions to overload the operators defined in parts a to c."


Last edited on
I'm not ENTIRELY sure this is the main cause of the issue, considering your error is "system could not find specified file", but after looking over your code I don't believe you can have const behind the declaration of an operator overloader.

As for your error, I can't see any apparent reason as to why you get this error with only the code you have referenced, but if you're using Visual Studio it wouldn't be a first if your entire project file is bugged. (Has happened to me tons of times, too)

I'd suggest trying to overload operators as such:
rectangleType operator+ (const rectangleType &);
(Same goes for any other operator overload and of course the actual declaration of the function in the .cpp file as well.)

(Hadn't noticed you were overloading comparison operators as well)
You would want to keep the const on those.
Last edited on
Is it possible your compile simply failed due to syntax somewhere? Or you already had it running in the background and tried to compile over it?

In the second case, the post-build copy to your final run-time directory would fail, which would result in a build error and possibly the "file not found error" when you tried to run it through debugger?!

Btw, for something like this, build in small pieces, incrementally. If there's a massive syntax error and it's difficult to determine where (because missing } or something silly), you could try to #if 0 giant blocks of code, closing it off with #endif
Syntax error:
1
2
3
4
5
ostream& operator<< (ostream& out, const rectangleType& rect)                
{
	out << "Length = " << rect.getLength << ", Width = " << rect.getWidth;
	return out;
}

You forgot parentheses. getLength() and getWidth() are functions.

Logical errors:

1. You wrote a 1, not an l. Perhaps also a good idea to name parameters 'length' and 'width'.
1
2
3
4
5
6
void rectangleType::setDimension(double l, double w)
{
	if (l >= 0)
		length = 1;
        // ...
}


2. Overloaded << but also wrote nearly identical print() method. Should remove print().
3. Didn't return anything in operator* overload.

Applied the fixes, simplified main(), and put project to https://repl.it/repls/NervousFALSERectangles

Put your constructors at the top of public, and at the top of the implementation file so that it's the first thing user sees.

Be conservative with the "using namespace std;" -- in main.cpp, go for it, but in header, qualify everything with std:: . It might be okay in rectangleType.cpp , because people don't usually include .cpp files, but I went conservative there and only specified the ones you were using.

I'm a bit curious about the reasoning behind setDimension() logic -- is it really important to have rect.setDimension(-13, 25) turn it into a (0, 25) rectangle instead of just throwing an error?

[Edit: added example to show post-increment operator based on pre-increment operator]
Last edited on
Topic archived. No new replies allowed.