gpt4 book ai didi

c++ - 如何在 C++ 中将 "new"用于多边形

转载 作者:行者123 更新时间:2023-11-28 02:34:37 24 4
gpt4 key购买 nike

Circle 的定义和 PolygonhereGraph.hgraph.cpp 中。

对于某些练习,我需要一些未命名的 shapes这是使用 new 制作的关键词。两者 Circlepolygon是种Shape .

例如,如果我有一个 vector_ref<Circle> vc;我可以使用此语句添加一个未命名的 Circle进入该 vector :vc.push_back(new Circle (Point (p), 50));因为我可以提供 circle 的参数(半径)定义它的时候。

但对于 polygons主题不同。因为有一个 polygon我必须先声明它,例如 Polygon poly;然后给它加点,这样,poly.add(Point(p)); .现在它给我带来了麻烦。

假设我有一个 polygons vector , Vector_ref<Polygon> vp;现在如何添加(即推回)polygon使用 new关键字就像我为 circle 做的一样请?我的代码是这样的:

#include <GUI.h>
using namespace Graph_lib;

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

class Math_shapes : public Window {

public:
Math_shapes(Point, int, int, const string&);

private:
//Widgets
Menu menu;
Button quit_button;
In_box x_coor;
In_box y_coor;

Vector_ref<Circle> vc;
Vector_ref<Graph_lib::Rectangle> vr;
Vector_ref<Graph_lib::Polygon> vt;
Vector_ref<Graph_lib::Polygon> vh;

//Action fucntions
void circle_pressed() {
int x = x_coor.get_int();
int y = y_coor.get_int();
vc.push_back(new Circle (Point(x,y), 50));
attach(vc[vc.size()-1]);
redraw();
}
void square_pressed() {
int x = x_coor.get_int();
int y = y_coor.get_int();
vr.push_back(new Graph_lib::Rectangle (Point(x,y), Point(x+100,y+100)));
attach(vr[vr.size()-1]);
redraw();
}
void triangle_pressed() {
int x = x_coor.get_int();
int y = y_coor.get_int();
vt.push_back(new Graph_lib::Polygon); // Problem is here!!
attach(vt[vt.size()-1]);
redraw();

}
void hexagon_pressed() {
int x = x_coor.get_int();
int y = y_coor.get_int();
Graph_lib::Polygon h;
h.add(Point(x,y)); h.add(Point(x+50,y+50)); h.add(Point(x+50,y+80));
h.add(Point(x,y+100)); h.add(Point(x-50,y+80)); h.add(Point(x-50,y+50));
vh.push_back(h);
attach(vh[vh.size()-1]);
redraw();
}

void quit() { hide(); }

// Call-back functions
static void cb_circle (Address, Address pw) { reference_to<Math_shapes>(pw).circle_pressed(); }
static void cb_square (Address, Address pw) { reference_to<Math_shapes>(pw).square_pressed(); }
static void cb_triangle (Address, Address pw) { reference_to<Math_shapes>(pw).triangle_pressed(); }
static void cb_hexagon (Address, Address pw) { reference_to<Math_shapes>(pw).hexagon_pressed(); }
static void cb_quit (Address, Address pw) { reference_to<Math_shapes>(pw).quit(); }
};

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

Math_shapes::Math_shapes(Point xy, int w, int h, const string& title):
Window(xy, w, h, title),
menu (Point(x_max()-150,70),120,30,Menu::vertical, "MathShapes"),
quit_button (Point(x_max()-100, 20), 70,20, "Quit", cb_quit),
x_coor(Point(x_max()-450,30),50,20,"x coordinate: "),
y_coor(Point(x_max()-250,30),50,20,"y coordinate: ")
{

attach(x_coor);
attach(y_coor);
attach(quit_button);
menu.attach(new Button(Point(0,0),0,0,"Circle",cb_circle));
menu.attach(new Button(Point(0,0),0,0,"Square",cb_square));
menu.attach(new Button(Point(0,0),0,0,"Equilateral triangle",cb_triangle));
menu.attach(new Button(Point(0,0),0,0,"Hexagon",cb_hexagon));
attach(menu);
}

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

int main()
try {
Math_shapes M_s(Point(100,100), 800, 600, "Math Shapes");
return gui_main();
}

catch(...)
{
return 0;
}

最佳答案

您只需将指针指向您的多边形,直到将其放入容器中:

Circle* pPoly = new Polygon();
// ...
pPoly->add(Point(p1));
// ...
pPoly->add(Point(p2));
// ...
vc.push_back(pPoly);

您可能想使用智能指针而不是上面那样的原始指针,但这是您可以开始的地方。

关于c++ - 如何在 C++ 中将 "new"用于多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27946996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com