Chapter 13 Exercise 1 - Can't Use Arc Class After Defining It

I have a problem with my implementation of class Arc for Chapter 13 Exercise 1. The book says to define it as class Arc, but there's another instance of something called "Arc" in there already so when I try to use it, the compiler can't tell that I mean the class I just defined. So I had to call the class Arc_class. Is there a way around this other having to use an alternative name?

Here are the exercise specs for reference:

Define a class Arc , which draws a part of an ellipse. Hint: fl_arc().


And here's my own code:
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
// Osman Zakir
// 5 / 8 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 13 Exercise 1
// Exercise Specifications:
/**
 * Define a class Arc , which draws a part of an ellipse. Hint: fl_arc().
 */

#include "../../cust_std_lib_facilities.h"
#include "../../Graph.h"
#include "../../Simple_window.h"

class Arc_class : Shape
{
public:
	Arc_class::Arc_class(double r, Point p, double start_deg, double end_deg)
		: m_r{ r }, m_p{ p }, m_start_deg{ start_deg }, m_end_deg{ end_deg }
	{
		fl_arc(m_p.x, m_p.y, m_r, m_start_deg, m_end_deg);
	}
	double get_radius() const { return m_r; }
	Point get_point() const { return m_p; }
	double set_start_deg(double theta) { m_start_deg = theta; }
	double set_end_deg(double theta) { m_end_deg = theta; }
private:
	double m_r;
	Point m_p;
	double m_start_deg;
	double m_end_deg;
};

int main()
{
	using namespace Graph_lib;

	constexpr int win_x = 100, win_y = 100;
	constexpr int win_width = 600, win_height = 400;
	Simple_window win{ Point{win_x, win_y}, win_width, win_height, "Arc" };

	const double radius1 = 4.0;

	const int arc1_x = 200, arc1_y = 200;
	const double start_theta = 0, end_theta = 180;

	Arc_class arc1{ radius1, Point{arc1_x, arc1_y}, start_theta, end_theta };
	win.attach(arc1);

	win.wait_for_button();
}


Edit: There's another problem now: I made it a derived class of Shape because I thought it'd be better that way as I'd be able to pass an [Arc_class[/code] object to Window::attach with that, but now it's giving me an error saying that I can't convert it to use base class Graph_lib::Shape. Please help.
Last edited on
Why don't you use your own namespace ?
Create my own namespace and put this class in there? That should help, yeah. I'll try that. But what about the problem of passing arc1 to win.attach() not working?

Edit: Right now my code is like this:
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
// Osman Zakir
// 5 / 8 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 13 Exercise 1
// Exercise Specifications:
/**
 * Define a class Arc , which draws a part of an ellipse. Hint: fl_arc().
 */

#include "../../cust_std_lib_facilities.h"
#include "../../Graph.h"
#include "../../Simple_window.h"

namespace Graph_lib
{
	class Arc : Shape
	{
	public:
		Arc::Arc(double r, Graph_lib::Point p, double start_deg, double end_deg)
			: m_r{ r }, m_p{ p }, m_start_deg{ start_deg }, m_end_deg{ end_deg }
		{
			add(Graph_lib::Point{ m_p.x - m_r, m_p.y - m_r });
		}
		void draw_lines() const;
		double get_radius() const { return m_r; }
		Graph_lib::Point get_point() const { return m_p; }
		double set_start_deg(double theta) { m_start_deg = theta; }
		double set_end_deg(double theta) { m_end_deg = theta; }
	private:
		double m_r;
		Graph_lib::Point m_p;
		double m_start_deg;
		double m_end_deg;
	};
}

int main()
{
	using namespace Graph_lib;

	constexpr int win_x = 100, win_y = 100;
	constexpr int win_width = 600, win_height = 400;
	Simple_window win{ Point{win_x, win_y}, win_width, win_height, "Arc" };

	const double radius1 = 4.0;

	const int arc1_x = 200, arc1_y = 200;
	const double start_theta = 0, end_theta = 180;

	Graph_lib::Arc arc1{ radius1, Point{arc1_x, arc1_y}, start_theta, end_theta };
	win.attach(arc1);

	win.wait_for_button();
}

void Arc::draw_lines() const
{
	using namespace Graph_lib;
	if (color().visibility())
	{
		fl_arc(point(0).x, point(0).y, m_r + m_r, m_r + m_r, m_start_deg, m_end_deg);
	}
}


When I mouse-over the mention of "arc1" on line 51, I get a dialogue box saying:
conversion to inaccessible base class "Graph_lib::Shape" is not allowed'.


Edit: Here's the error I get when I try to compile the code:

chapter13ex1.cpp(51): error C2243: 'type cast': conversion from 'Graph_lib::Arc *' to 'Graph_lib::Shape &' exists, but is inaccessible


And here's the updated code:
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
// Osman Zakir
// 5 / 8 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 13 Exercise 1
// Exercise Specifications:
/**
 * Define a class Arc , which draws a part of an ellipse. Hint: fl_arc().
 */

#include "../../cust_std_lib_facilities.h"
#include "../../Graph.h"
#include "../../Simple_window.h"

namespace Graph_lib
{
	class Arc : Shape
	{
	public:
		Arc::Arc(double r, Graph_lib::Point p, double start_deg, double end_deg)
			: m_r{ r }, m_p{ p }, m_start_deg{ start_deg }, m_end_deg{ end_deg }
		{
			add(Graph_lib::Point{ int(m_p.x - m_r), int(m_p.y - m_r) });
		}
		void draw_lines() const;
		double get_radius() const { return m_r; }
		Graph_lib::Point get_point() const { return m_p; }
		double set_start_deg(double theta) { m_start_deg = theta; }
		double set_end_deg(double theta) { m_end_deg = theta; }
	private:
		double m_r;
		Graph_lib::Point m_p;
		double m_start_deg;
		double m_end_deg;
	};
}

int main()
{
	using namespace Graph_lib;

	constexpr int win_x = 100, win_y = 100;
	constexpr int win_width = 600, win_height = 400;
	Simple_window win{ Point{win_x, win_y}, win_width, win_height, "Arc" };

	const double radius1 = 4.0;

	const int arc1_x = 200, arc1_y = 200;
	const double start_theta = 0, end_theta = 180;

	Graph_lib::Arc arc1{ radius1, Point{arc1_x, arc1_y}, start_theta, end_theta };
	win.attach(arc1);

	win.wait_for_button();
}

void Arc::draw_lines() const
{
	using namespace Graph_lib;
	if (color().visibility())
	{
		fl_arc(point(0).x, point(0).y, int(m_r + m_r), int(m_r + m_r), m_start_deg, m_end_deg);
	}
}


Edit: Okay, it worked after I changed it into a struct that's derived from Shape. The book says to define a class Arc, but I think this should also work since a struct is also a type of class in C++ (right?).

Here's my code (it works):
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
// Osman Zakir
// 5 / 8 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 13 Exercise 1
// Exercise Specifications:
/**
 * Define a class Arc , which draws a part of an ellipse. Hint: fl_arc().
 */

#include "../../cust_std_lib_facilities.h"
#include "../../Graph.h"
#include "../../Simple_window.h"

namespace Graph_lib
{
	struct Arc : Shape
	{
		Arc::Arc(double r, Graph_lib::Point p, double start_deg, double end_deg)
			: m_r{ r }, m_p{ p }, m_start_deg{ start_deg }, m_end_deg{ end_deg }
		{
			add(Graph_lib::Point{ int(m_p.x - m_r), int(m_p.y - m_r) });
		}
		void draw_lines() const;
		double get_radius() const { return m_r; }
		Graph_lib::Point get_point() const { return m_p; }
		double set_start_deg(double theta) { m_start_deg = theta; }
		double set_end_deg(double theta) { m_end_deg = theta; }
	private:
		double m_r;
		Graph_lib::Point m_p;
		double m_start_deg;
		double m_end_deg;
	};
}

int main()
{
	using namespace Graph_lib;

	constexpr int win_x = 100, win_y = 100;
	constexpr int win_width = 600, win_height = 400;
	Simple_window win{ Point{win_x, win_y}, win_width, win_height, "Arc" };

	const double radius1 = 100.0;

	const int arc1_x = 200, arc1_y = 200;
	const double start_theta = 0, end_theta = 180;

	Graph_lib::Arc arc1{ radius1, Point{arc1_x, arc1_y}, start_theta, end_theta };
	arc1.set_color(Color::black);
	win.attach(arc1);

	const double radius2 = 100.0;

	const int arc2_x = 250, arc2_y = 300;
	const double start_theta2 = 0, end_theta2 = 75;

	Graph_lib::Arc arc2{ radius2, Point{arc2_x, arc2_y}, start_theta2, end_theta2 };
	arc2.set_color(Color::black);
	win.attach(arc2);

	win.wait_for_button();
}

void Arc::draw_lines() const
{
	using namespace Graph_lib;
	if (color().visibility())
	{
		fl_arc(point(0).x, point(0).y, int(m_r + m_r), int(m_r + m_r), m_start_deg, m_end_deg);
	}
}
Last edited on
Topic archived. No new replies allowed.