How to send a Rectangle to a function which takes a rectangle argument, in C++

Pages: 12
Hello guys,

Consider I have a rectangle like this:

1
2
3
Point p1(100,100);
Point p2(300,200);
Graph_lib::Rectangle r(p1,p2);


And also I have a function that takes a rectangle and returns a Point of that, say, top-left corner of that rectangle. For example:

Point N(Graph_lib::Rectangle R1);

My question is that, first, how to send that Rectangle r(p1,p2) to the Point N(Graph_lib::Rectangle R1) function? And then, how to return a Point from that function?

My IDE is visual studio 2012.
how to send that Rectangle r(p1,p2) to the Point N(Graph_lib::Rectangle R1) function?
N(r)

how to return a Point from that function?
return this_is_a_Point;
closed account (j3Rz8vqX)
And you would assign it like this:

top_corner = N(r);//Assigns the returning value to variable top_corner
Helo all i need a programe....topic is filling here i need to add the data of five students their mobile number,address,sec, and deprtmnt....program should be like
s.no name address phone
1.
2.
3.
4.
5.
and starting should be like press 1 to insert data
press 2 to save data
press 3 to find data
press 4 to quit
@helios:

I wrote 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
#include <Simple_window.h>

Point  N(Graph_lib::Rectangle r);

//*******************************************************

int main()
{
	using namespace Graph_lib; 
   
    Simple_window win(Point(100,100), 600,400, "Connection_Points");	
	Point p1(200,100);
	Point p2(400,200);
	Graph_lib::Rectangle r (p1,p2);
	Mark m(N(r),'x');
	win.attach(m);
    win.wait_for_button();
}

//*******************************************************************

Point n(Graph_lib::Rectangle r)
{
	return this_is_a_Point;
}


But I got three errors:

Error 8 error C2248: 'Graph_lib::Shape::Shape' : cannot access private member declared in class 'Graph_lib::Shape' c:\program files\microsoft visual studio 11.0\vc\include\graph.h 212

Error 9 error C2065: 'this_is_a_Point' : undeclared identifier c:\users\cs\documents\visual studio 2012\projects\test\test\test.cpp 33

10 IntelliSense: identifier "this_is_a_Point" is undefined c:\Users\CS\Documents\Visual Studio 2012\Projects\test\test\test.cpp 33
this_is_a_Point hasn't been declared anywhere, you need to declare and initialize it before you can use it. Helios didn't literally mean return "this_is_a_Point" but rather create a point then return that point.
Last edited on
saddozai wrote:
Helo all i need a programe...


1) Please don't hijack other people's threads. Start your own thread, if you have a problem you want to solve that's not related to the current thread.

2) This isn't a free homework service. When you start your own thread, please post the code you've already written, and we'll help you figure out any problems you're having with that code.
Why no one can solve that problem? So how the author of PPP has written it for learners?
Mike5424 has already explained the 2nd and 3rd errors you're getting. You've simply copied and pasted some pseudo-code as it were working C++ code. You didn't actually make any effort to understand what it was helios wrote, did you?

As for your first error, you haven't shown us nearly enough code for us to be sure. We'd need to see your definition of Rectangle, and of Shape (which, presumably, is a base class of Rectangle).

My guess, based on limited information, is that you're attempting to access a constructor of Shape that's declared as private. Maybe a default constructor? Or a copy constructor? Maybe it's because your functions N and n take a Rectangle parameter by value, which means that an attempt is being made to call a copy constructor which has been declared as private?

Incidentally, C++ is case-sensitive. n and N are not the same thing.

Edit: I don't understand your final question. What do you mean by "PPP"?
Last edited on
Yes I did, but he wrote it like a real code and since I'm not expert like you (!) I couldn't to find that it is throwing me to a misunderstanding.


But surely you could see that no entity called this_is_a_Point has been defined anywhere in your code?
This is the code of Shape in Graph.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
32
33
class Shape  {        // deals with color and style, and holds sequence of lines 
public:
    void draw() const;                 // deal with color and draw lines
    virtual void move(int dx, int dy); // move the shape +=dx and +=dy

    void set_color(Color col) { lcolor = col; }
    Color color() const { return lcolor; }
    void set_style(Line_style sty) { ls = sty; }
    Line_style style() const { return ls; }
    void set_fill_color(Color col) { fcolor = col; }
    Color fill_color() const { return fcolor; }

    Point point(int i) const { return points[i]; } // read only access to points
    int number_of_points() const { return int(points.size()); }

    virtual ~Shape() { }
protected:
    Shape();    
    virtual void draw_lines() const;   // draw the appropriate lines
    void add(Point p);                 // add p to points
    void set_point(int i,Point p);     // points[i]=p;
private:
    vector<Point> points;              // not used by all shapes
    Color lcolor;                      // color for lines and characters
    Line_style ls; 
    Color fcolor;                      // fill color

    Shape(const Shape&);               // prevent copying
    Shape& operator=(const Shape&);
};

//------------------------------------------------------------------------------


And this is for Rectangle:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct Rectangle : Shape {

    Rectangle(Point xy, int ww, int hh) : h(hh), w(ww)
    {
        add(xy);
        if (h<=0 || w<=0) error("Bad rectangle: non-positive side");
    }

    Rectangle(Point x, Point y) : h(y.y-x.y), w(y.x-x.x)
    {
        add(x);
        if (h<=0 || w<=0) error("Bad rectangle: non-positive width or height");
    }
    void draw_lines() const;

    int height() const { return h; }
    int width() const { return w; }
private:
    int h;    // height
    int w;    // width
};
I revised the code. It's as below now:

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
#include <Simple_window.h>

Point  n(Graph_lib::Rectangle r);
Point  s(Graph_lib::Rectangle r);
Point  e(Graph_lib::Rectangle r);
Point  w(Graph_lib::Rectangle r);
Point ne(Graph_lib::Rectangle r);
Point se(Graph_lib::Rectangle r);
Point nw(Graph_lib::Rectangle r);
Point sw(Graph_lib::Rectangle r);
Point center(Graph_lib::Rectangle r);

//*******************************************************

int main()
{
	using namespace Graph_lib; 
   
    Simple_window win(Point(100,100), 600,400, "Connection_Points");	
	Point p1(200,100);
	Point p2(400,200);
	Graph_lib::Rectangle r (p1,p2);
	Mark m(n(r),'x');
	win.attach(m);
    win.wait_for_button();
}

//*******************************************************************

Point n(Graph_lib::Rectangle r)
{
	return r.point(0);
}

//*************************************************

Point  s(Graph_lib::Rectangle r);

//*************************************************

Point  e(Graph_lib::Rectangle r);

//*************************************************

Point  w(Graph_lib::Rectangle r);

//*************************************************

Point ne(Graph_lib::Rectangle r);

//*************************************************

Point se(Graph_lib::Rectangle r);

//*************************************************

Point nw(Graph_lib::Rectangle r);

//*************************************************

Point sw(Graph_lib::Rectangle r);

//*************************************************

Point center(Graph_lib::Rectangle r);


And the only error is:
Error 8 error C2248: 'Graph_lib::Shape::Shape' : cannot access private member declared in class 'Graph_lib::Shape' c:\program files\microsoft visual studio 11.0\vc\include\graph.h 212


PPP is this book: http://www.stroustrup.com/Programming/
So if you look at line 28 of the definition of Shape, it looks as though my guess was correct in my previous post:

MikeyBoy wrote:
My guess, based on limited information, is that you're attempting to access a constructor of Shape that's declared as private. Maybe a default constructor? Or a copy constructor? Maybe it's because your functions N and n take a Rectangle parameter by value, which means that an attempt is being made to call a copy constructor which has been declared as private?


The copy constructor for Shape is private, which means that all objects which derive from Shape (e.g. Rectangle) are non-copyable.

Since you're attempting to pass a Rectangle into n() by value, the copy constructor for Rectangle is being invoked. This, in turn, attempts to invoke the copy constructor of the base class, Shape. However, that copy constructor is private, so it's illegal.

rectangle.h line 1. You did not declare an access level for the inheiritance.

If no access level is specified for the inheritance, the compiler assumes private for classes declared with keyword class and public for those declared with struct.
http://www.cplusplus.com/doc/tutorial/inheritance/

Since Shape is a class, the default access level is private and therefore Rectangle does not have access to Shape's constructor since Shape's constructor is protected.


Last edited on
AbstractionAnon wrote:
Since Shape is a class, the default access level is private and therefore Rectangle does not have access to Shape's constructor since Shape's constructor is protected.

Oops, yes, you're right. I didn't spot that.

But even once that's fixed, it's going to be illegal to pass a Rectangle by value into a function, as the copy constructor for Shape is private.
Dear MikeyBoy:

You solved the problem.
I put a '&' and the problem became solved.
Thank you very much.
You're welcome! I'm glad it worked :)
I used r.point(0); in line 32 to point to the top-left corner of the rectangle. I need the height and the width of that rectangle in other functions. How to use the height and width of r in those functions?
Last edited on
I need the height and the width of that rectangle in other functions. How to use the height and width of r in those functions?

At lines 16 and 17 of your Rectangle definition, you've defined methods to return the height and the width.
Pages: 12