Rectangle class

"Write a class definition named rectangle to represent a rectangle. The class contains
1. Two private double fields named width and height that specify the width and height of rectangle.
2. A string data field named color that specifies the color of the rectangle. The default color is white.
3. A no-argument constructor that creates a default rectangle
4. A constructor that creates a rectangle with the specified width and height
5. A member function named get_area( ) that returns the area of this rectangle
6. A member function named get_perimeter( ) that returns the perimeter of this rectangle
7. A member function named compare( rectangle another) that compares area of this rectangle with passed in rectangle and returns true if this rectangle is greater than passed in rectangle otherwise false
Pass object as argument"


I get multiple errors when running the program.

error C2535: 'rectangle::rectangle(double,double)' : member function already defined or declared

error C3867: 'rectangle::get_area': function call missing argument list; use '&rectangle::get_area' to create a pointer to member

error C3867: 'rectangle::get_perimeter': function call missing argument list; use '&rectangle::get_perimeter' to create a pointer to member

error C3867: 'rectangle::get_area': function call missing argument list; use '&rectangle::get_area' to create a pointer to member

error C3867: 'rectangle::get_perimeter': function call missing argument list; use '&rectangle::get_perimeter' to create a pointer to member

I don't quite understand the errors.



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
  #include <iostream>
#include <string> 
using namespace std; 

class rectangle
{
private:
	double width;
	double height;
	string color;

public:
	rectangle(double w, double h)
	{
		width = 0;
		height = 0;
	}
	rectangle( double w, double h)
	{
			height = h;
			width= w;
			color = "white";
	}


double set_width(double w)
{
	width = w;
}

double set_height(double h)
{
	height = h;
}

double get_area()
{
	return width * height;
}


double get_perimeter()
{
	return 2 * (width +  height);
}

void compare(rectangle r1, rectangle r2)
{
	if (r2.get_area() > r1.get_area())
	{
		cout << "Rectangle 2 area is greater than Rectangle 1.";
	}
	else if ((r2.get_area() < r1.get_area()))
	{
		cout << "Rectangle 1 area ia greater than Rectangle 2.";
	}
}
};
int main()
{
	rectangle r1();
	rectangle r2(78.5, 23.4);
	r1().set_width(25.7);
	r1().set_height(37.2);
	r2.compare (r1(), r2);
	cout << "The area of rectangle 1 is: " <<r1().get_area << endl;
	cout << "The perimeter of rectangle 1 is: " <<r1().get_perimeter << endl;
	cout << "The area of rectangle 2 is: " << r2.get_area << endl;
	cout << "The perimteter of rectangle 2 is: " << r2.get_perimeter << endl; 

	return 0;
}
Lines 13-23: You defined the same constructor twice.

Lines 66-69: Function calls always require parenthesis () even if they are empty due to not taking any arguments.
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
#include <iostream>
#include <string> 
using namespace std; 

class rectangle
{
private:
	double width;
	double height;
	string color;

public:
	rectangle(double w, double h)
	{
		width = 0;
		height = 0;
	}
	rectangle(string color, double w, double h)
	{
			height = h;
			width= w;
			color = "white";
	}


double set_width(double w)
{
	width = w;
}

double set_height(double h)
{
	height = h;
}

double get_area()
{
	return width * height;
}


double get_perimeter()
{
	return 2 * (width +  height);
}

void compare(rectangle r1, rectangle r2)
{
	if (r2.get_area() > r1.get_area())
	{
		cout << "Rectangle 2 area is greater than Rectangle 1.";
	}
	else if ((r2.get_area() < r1.get_area()))
	{
		cout << "Rectangle 1 area ia greater than Rectangle 2.";
	}
}
};
int main()
{
	rectangle r1();
	rectangle r2(78.5, 23.4);
	r1().set_width(25.7);
	r1().set_height(37.2);
	r2.compare (r1(), r2);
	cout << "The area of rectangle 1 is: " <<r1().get_area << endl;
	cout << "The perimeter of rectangle 1 is: " <<r1().get_perimeter << endl;
	cout << "The area of rectangle 2 is: " << r2.get_area << endl;
	cout << "The perimteter of rectangle 2 is: " << r2.get_perimeter << endl; 

	return 0;
}

I'm not sure what you mean for lines 66-69.
On line 49 you do write r1.get_area() but on line 66 you write r1().get_area. Do you notice the difference?
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
#include <iostream>
#include <string> 
using namespace std; 

class rectangle
{
private:
	double width;
	double height;
	string color;

public:
	rectangle(double w, double h)
	{
		width = 0;
		height = 0;
	}
	rectangle(string color, double w, double h)
	{
			height = h;
			width= w;
			color = "white";
	}


double set_width(double w)
{
	width = w;
	return w;
}

double set_height(double h)
{
	height = h;
	return h; 
}

double get_area()
{
	return width * height;
}


double get_perimeter()
{
	return 2 * (width +  height);
}

void compare(rectangle r1, rectangle r2)
{
	if (r2.get_area() > r1.get_area())
	{
		cout << "Rectangle 2 area is greater than Rectangle 1.";
	}
	else if ((r2.get_area() < r1.get_area()))
	{
		cout << "Rectangle 1 area ia greater than Rectangle 2.";
	}
}
};
int main()
{
	rectangle r1();
	rectangle r2(78.5, 23.4);
	r1().set_width(25.7);
	r1().set_height(37.2);
	r2.compare (r1(),r2);
	cout << "The area of rectangle 1 is: " <<r1().get_area() << endl;
	cout << "The perimeter of rectangle 1 is: " <<r1().get_perimeter() << endl;
	cout << "The area of rectangle 2 is: " << r2.get_area() << endl;
	cout << "The perimteter of rectangle 2 is: " << r2.get_perimeter() << endl; 

	return 0;
}



Error 1 error LNK2019: unresolved external symbol "class rectangle __cdecl r1(void)" (?r1@@YA?AVrectangle@@XZ) referenced in function _main
Error 2 error LNK1120: 1 unresolved externals


After fixing a couple of things, I'm getting these two errors.
You can just do r1.setheight(37.2)
Last edited on
Line 63. Do you think that you declare a variable? No, you don't. You declare a function, whose name is "r1", who does not take any parameters, and returns an class rectangle object. "If it could be a function declaration, then it is a function declaration."

Remove those parentheses (line 63).
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
#include <iostream>
#include <string> 
using namespace std; 

class rectangle
{
private:
	double width;
	double height;
	string color;

public:
	rectangle(double w, double h)
	{
		width = 0;
		height = 0;
	}
	rectangle(string color, double w, double h)
	{
			height = h;
			width= w;
			color = "white";
	}


double rectangle::set_width(double w)
{
	width = w;
	return w;
}

double rectangle::set_height(double h)
{
	height = h;
	return h; 
}

double get_area()
{
	return width * height;
}


double get_perimeter()
{
	return 2 * (width +  height);
}

void compare(rectangle r1, rectangle r2)
{
	if (r2.get_area() > r1.get_area())
	{
		cout << "Rectangle 2 area is greater than Rectangle 1.";
	}
	else if ((r2.get_area() < r1.get_area()))
	{
		cout << "Rectangle 1 area ia greater than Rectangle 2.";
	}
}
};
int main()
{
	rectangle r1();
	rectangle r2(78.5, 23.4);
	r1().set_width(25.7);
	r1().set_height(37.2);
	r2.compare (r1(),r2);
	cout << "The area of rectangle 1 is: " <<r1().get_area() << endl;
	cout << "The perimeter of rectangle 1 is: " <<r1().get_perimeter() << endl;
	cout << "The area of rectangle 2 is: " << r2.get_area() << endl;
	cout << "The perimteter of rectangle 2 is: " << r2.get_perimeter() << endl; 

	return 0;
}


It didn't fix anything.
Oh Im sorry, I didnt see they were defined inside your class. Remove the rectangle:: again :p But normally you would have them since definitions are in seperate files.

Read what @keskiverto said
Last edited on
When I remove those parentheses on line 63, it gives me multiple errors.
It reveals those other errors. The line 63 error did hide the others. The LNK**** are linker errors.

Now you should have compiler errors that mention the lines, where the syntax error occurs. Lines 65, 66, 68 and 69, I presume.

If line 68 has error but line 70 does not, then how do they differ?
rectangle r1();

You dont have a default constructor so you cant do that.
Add : rectangle(){} to your class

1
2
r1().set_width(25.7);
r1().set_height(37.2);


Remove parenthesis.

r2.compare (r1(),r2);

Remove parenthesis on r1.

1
2
cout << "The area of rectangle 1 is: " <<r1().get_area() << endl;
cout << "The perimeter of rectangle 1 is: " <<r1().get_perimeter() << endl;


Remove parenthesis on r1.
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
#include <iostream>
#include <string> 
using namespace std; 

class rectangle(){}
{
private:
	double width;
	double height;
	string color;

public:
	rectangle(double w, double h)
	{
		width = 0;
		height = 0;
	}
	rectangle(string color, double w, double h)
	{
			height = h;
			width= w;
			color = "white";
	}


double set_width(double w)
{
	width = w;
	return w;
}

double set_height(double h)
{
	height = h;
	return h; 
}

double get_area()
{
	return width * height;
}


double get_perimeter()
{
	return 2 * (width +  height);
}

void compare(rectangle r1, rectangle r2)
{
	if (r2.get_area() > r1.get_area())
	{
		cout << "Rectangle 2 area is greater than Rectangle 1.";
	}
	else if ((r2.get_area() < r1.get_area()))
	{
		cout << "Rectangle 1 area ia greater than Rectangle 2.";
	}
}
};
int main()
{
	rectangle r1();
	rectangle r2(78.5, 23.4);
	r1.set_width(25.7);
	r1.set_height(37.2);
	r2.compare (r1,r2);
	cout << "The area of rectangle 1 is: " <<r1.get_area() << endl;
	cout << "The perimeter of rectangle 1 is: " <<r1.get_perimeter() << endl;
	cout << "The area of rectangle 2 is: " << r2.get_area() << endl;
	cout << "The perimteter of rectangle 2 is: " << r2.get_perimeter() << endl; 

	return 0;
}


Okay, I changed the rectangle class, removed the parentheses, but still getting multiple errors
class rectangle(){}

Sorry but I found this quite funny :D

Its suppose to be class rectangle just like you had it before.
What I meant was, adding rectangle(){} as a constructor in the public thingy

1
2
public:
rectangle(){}


Because, Here - rectangle r1; // remove the parenthesis You are calling the default constructor, and if you dont add what I told you to add, then you'd have no default constructor to call for, (a defualt constructor is a constructor with no parameters)

I'm still getting errors with r1 in the main function.
Last edited on
You didint change Anything I told you to. Read my post again^^
It runs, but this is the output I get:

Rectangle 1 area ia greater than Rectangle 2.
The area of rectangle 1 is: 956.04
The perimeter of rectangle 1 is: 125.8
The area of rectangle 2 is: 0
The perimteter of rectangle 2 is: 0
Press any key to continue . . .

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
#include <iostream>
#include <string> 
using namespace std; 

class rectangle
{
private:
	double width;
	double height;
	string color;

public:
	rectangle(double w, double h)
	{
		width = 0;
		height = 0;
	}
	rectangle(string color, double w, double h)
	{
			height = h;
			width= w;
			color = "white";
	}
public:

	rectangle(){}

double set_width(double w)
{
	width = w;
	return w;
}

double set_height(double h)
{
	height = h;
	return h; 
}

double get_area()
{
	return width * height;
}


double get_perimeter()
{
	return 2 * (width +  height);
}

void compare(rectangle r1, rectangle r2)
{
	if (r2.get_area() > r1.get_area())
	{
		cout << "Rectangle 2 area is greater than Rectangle 1.";
	}
	else if (r2.get_area() < r1.get_area())
	{
		cout << "Rectangle 1 area ia greater than Rectangle 2.";
	}
}
};
int main()
{
	rectangle r1;
	rectangle r2(78.5, 23.4);
	r1.set_width(25.7);
	r1.set_height(37.2);
	r2.compare (r1,r2);
	cout << "The area of rectangle 1 is: " <<r1.get_area() << endl;
	cout << "The perimeter of rectangle 1 is: " << r1.get_perimeter() << endl;
	cout << "The area of rectangle 2 is: " << r2.get_area() << endl;
	cout << "The perimteter of rectangle 2 is: " << r2.get_perimeter() << endl; 

	return 0;
}




Should I prompt the user to enter a width and height for rectangle 2? And if so, where?
Last edited on
Topic archived. No new replies allowed.