如何发送矩形到一个函数,它接受一个矩形参数,在C ++ [英] How to send a Rectangle to a function which takes a rectangle argument, in C++

查看:143
本文介绍了如何发送矩形到一个函数,它接受一个矩形参数,在C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑我有一个这样的矩形:

 点p1(100,100) 
Point p2(300,200);
Graph_lib :: Rectangle r(p1,p2);

还有一个函数接受一个矩形并返回一个Point,该矩形的左角。例如:

 点N(Graph_lib :: Rectangle R1); 

我的问题是,首先,如何发送 Rectangle r ,p2)点N(Graph_lib :: Rectangle R1)函数?然后,如何从该函数返回一个点?



我的IDE是visual studio 2012.
这是我跑的代码,得到的错误:
错误8错误C2248:'Graph_lib :: Shape :: Shape':无法访问在类'Graph_lib :: Shape'中声明的私有成员c:\program files\microsoft visual studio 11.0 \vc\\ \\include\graph.h 212

  #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);
点中心(Graph_lib :: Rectangle r);

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

int main()
{
使用命名空间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);
}

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

点数(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);

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

点sw(Graph_lib :: Rectangle r);

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

点中心(Graph_lib :: Rectangle r);


解决方案

您只需要指定参数,

例如

 点p1 100,100); 
Point p2(300,200);
Graph_lib :: Rectangle r(p1,p2);

点p3 = N(r);

编辑:您似乎正在谈论B. Straustrup 。



在这种情况下,函数可以用下面的方式定义:

  Point N(const Graph_lib :: Rectangle& R1)
{
return R1.point(0);
}

只有我不知道struct Point是否在命名空间Graph_lib中定义。如果是这样,则函数将被定义为

  Graph_lib :: Point N(const Graph_lib :: Rectangle& R1)
{
return R1.point(0);
}

  Graph_lib :: Point TopLeftPoint(const Graph_lib :: Rectangle& r)
{
return r.point(0);
}


Consider I have a rectangle like this:

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. This is the code I ran and got the error: 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

#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);

解决方案

You simply need to specify the argument instead of the function parameter when you call the function

For example

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

Point p3 =  N( r );

EDIT: It seems you are speaking about the book by B. Straustrup.

In this case the function can be defined the following way

Point N( const Graph_lib::Rectangle &R1 )
{
   return R1.point( 0 );
}

Only I do not know whether struct Point is defined in namespace Graph_lib. If so then the function will be defined as

Graph_lib::Point N( const Graph_lib::Rectangle &R1 )
{
   return R1.point( 0 );
}

Or

Graph_lib::Point TopLeftPoint( const Graph_lib::Rectangle &r )
{
   return r.point( 0 );
}

这篇关于如何发送矩形到一个函数,它接受一个矩形参数,在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆