【问题标题】:How to send a Rectangle to a function which takes a rectangle argument, in C++如何在 C++ 中将矩形发送到接受矩形参数的函数
【发布时间】:2014-05-13 07:22:10
【问题描述】:

假设我有一个这样的矩形:

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

我还有一个函数,它接受一个矩形并返回一个点,比如说,那个矩形的左上角。例如:

Point N(Graph_lib::Rectangle R1);

我的问题是,首先,如何将 Rectangle r(p1,p2) 发送到 Point 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);
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);

【问题讨论】:

  • 请阅读任何 C++ 入门书籍或教程,这将在此处进行介绍。
  • @Mat 奇怪的是,当我对一个展示相同知识/研究水平的问题提出相同的建议时,我被半数的 stackoverflow 社区斥责为粗鲁。名声不足?
  • @o_weisman 也许是最暴躁的 SO 用户现在都睡着了?
  • @Mat: 不,我有 PPP 书,这是一个练习。到目前为止,还没有关于这个问题的任何消息。不幸的是,有人对这个问题投了反对票。很抱歉,我使用 stackoverflow.com 寻求帮助。
  • @abbasi:Stack Overflow 是针对特定编程问题的,它不能替代书籍、培训等。您所问的内容包含在 C++ 的基本学习材料中(所有其他语言都一样)。请花一些时间阅读至少一本书或综合教程。

标签: c++ visual-c++


【解决方案1】:

你只需要在调用函数时指定实参而不是函数参数

例如

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

【讨论】:

  • 这很简单,弗拉德。我有两个问题,第二个是如何从该函数返回一个点。你已经把它留在了答案中。
  • @abbasi 您的第二个问题不正确。听起来应该是如何定义函数的。我不知道矩形是如何定义的,所以我无话可说。显示矩形的定义。
  • 所以你的回答毫无意义。这是 PPP 第 477 页练习的确切上下文:定义函数 n()、s()、e()、w()、center()、ne()、se()、sw()和 nw()。每个都接受一个 Rectangle 参数并返回一个 Point。这些函数定义矩形上和矩形内的连接点。例如,nw(r) 是称为 r 的矩形的西北(左上角)。
  • @abbasi 我不知道你在说什么书。但无论如何,如果你想得到一个完整的答案,你必须提出一个完整的问题。
  • @Vlad:我也写了那个代码,但是出错了。主要的是,Error 8 error C2248: 'Graph_lib::Shape::Shape' : cannot access private member declaration in class 'Graph_lib::Shape' c:\program files\microsoft visual studio 11.0\vc\include \graph.h 212.
【解决方案2】:

简单的矩形是:

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

【讨论】:

  • 你刚刚写了 C#,兄弟。
  • 或 Java,但看起来不像 C++。
  • 这行不通,兄弟。我在下一条评论中给出了一些关于 Rectangle 的解释。
  • 它是用c++编写的
  • 在 PPP 书中,只有(直到本章)定义矩形的两种方法。 Rectangle(Point xy, int h, int w) 或 _ Rectangle(Point x, Point y)_。还有那个 Point(10,30); 真的没有意义。
【解决方案3】:

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

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++ 行中得到解答。 :)

【讨论】:

  • 首先,您还没有阅读过相互交换的 cmets 以及弗拉德给/与我的答案。其次,您还没有阅读完整的深刻含义的问题。第三,回答确实意味着回答而不是半回答。感谢您教我这些术语。
  • 你是真的吗?我引用了你的问题!您可以自己阅读它们。现在我想知道为什么我浪费时间回答一个有这种态度的人的问题!
  • 关于您回答我问题所花费的时间,您是对的。这是一种善意。但我知道我在说什么,他们都是对的。我也想知道为什么当有人指出别人的错误时,那个人应该给他,作为回报,反对票!没问题的人。我有-6,如果我有-60,我仍然不在乎。正如我所说,问题现在结束了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多