如何在C ++中将Rectangle发送到带有矩形参数的函数

时间:2014-05-13 07:22:10

标签: c++ visual-c++

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

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

此外,我还有一个带矩形的函数,并返回一个Point,例如,该矩形的左上角。例如:

Point N(Graph_lib::Rectangle R1);

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

我的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);
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);

3 个答案:

答案 0 :(得分:2)

调用函数

时,只需要指定参数而不是函数参数

例如

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

Point 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 );
}

答案 1 :(得分:0)

简单的矩形是:

Rect(int x, int y, int width, int height);

您的功能定义为:

Point N(Rectangle rect)
{
  Point anypt = new Point(10,30);
  return anypt;
}

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

您可以调用函数N并将参数矩形r传递为:

点p = N(r);

答案 2 :(得分:0)

  

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

A1

正确的术语是“通过”,而不是“发送”。 - 您需要将类型为Graph_lib::Rectangle的对象传递给名为N的函数,该函数返回一个Point对象。从你对数学的了解来考虑这一点。想想简单的函数f(x) = 2*x。如果传递数字2作为函数f的参数,它将返回一个整数4。

现在让我们回到这个问题。以下是简单的C ++代码作为第一个问题的答案:

// Note: this is your own code
Point p1(100, 100);
Point p2(300, 200);
Graph_lib::Rectangle rec(p1, p2);
// Now let's call the function N and get the Point from it
Point resultPoint = N(rec); // Here we PASS the rec object to the function N()

瞧!现在你可以使用Point类型的resultPoint变量。

<强> A2

在我们称之为N()函数的同一C ++行中回答了问题2。 :)