In C++ by FLTK, to define a circle we use some code like this:
Circle c(Point(x,y), r);
And we can using vector_ref put and save them in a vector, like:
Vector_ref<Circle> vc;
vc.push_back(new Circle(Point(x,y),r));
OK, those were about Circle and no problem till now! Triangle can be defined like this for using in codes:
Graph_lib::polygon poly;
poly.add(Point(x1,y1),r1);
poly.add(Point(x2,y2),r2);
poly.add(Point(x3,y3),r3);
and this is a vector to saving them:
Vector_ref<Graph_lib::Polygon> vp;
The problem is that how to save/put triangles/polygons
into that vp vector using new
keyword like the circle does?
The code I used as an answer to the exercise no 12 (of here) is this:
/* The binary-tree class, one of the topics that has been said in Programming Principles and Practice using C++ book by Biarne Stroustrup.
This code is written by R Abbasi (s.rabbasi@yahoo.com) */
#include <Simple_window.h>
#include <iostream>
vector <Graph_lib::Polygon> vpo;
vector <Point> vp;
int pow(int);
class Binary_tree: public Shape {
public:
Binary_tree(Point _p, int l):level(l), p(_p) {
preparing(); }
void preparing();
void put_nodes(Point);
void wheel(Point);
void make_nodes(Point);
void draw_lines() const {
for(int i = 0; i*2+2 < vp.size(); i++) {
fl_line(vp[i].x,vp[i].y, vp[i*2+1].x,vp[i*2+1].y);
fl_line(vp[i].x,vp[i].y, vp[i*2+2].x,vp[i*2+2].y);
}
}
private:
Point p;
int i, j, k, level;
double scale;
};
//**********************************
void Binary_tree::preparing() {
if(level < 1) error("Bad inputted level!");
else if (level == 1) put_nodes(p);
else {
scale = 5 * pow(level);
i = 1; j = 1; k = 3;
put_nodes(p);
make_nodes(p);
}
}
//***************************************
void Binary_tree::put_nodes(Point p) {
vp.push_back(p);
Graph_lib::Polygon poly;
poly.add(Point(p.x-2,p.y));
poly.add(Point(p.x,p.y-3));
poly.add(Point(p.x+3,p.y));
vpo.push_back(&poly);
}
//******************************************
void Binary_tree::wheel(Point p) {
put_nodes(Point(p.x - scale, p.y+30));
put_nodes(Point(p.x + scale, p.y+30));
}
//*****************************************
void Binary_tree::make_nodes(Point p) {
while(vp.size() < (pow(k)-1))
wheel(vp[vp.size()-i++]);
if(i < pow(level)) {
k++;
scale *= 1.0/2.0;
make_nodes(vp[vp.size()-i]);
}
}
//*********************
int pow(int l) {
int m = 2;
for(int k = 2; k < l; k++) m *= 2;
return m;
}
//***************************************
int main() try
{
Simple_window win(Point(),1300,500, "Binary_tree");
int level;
cout<< "Please enter the level of the Binary-tree:";
if(!(cin>>level)) error("Bad number of level!");
Point p(10*pow(level),20);
Binary_tree b_t(p,level);
vpo[0].set_color(Color::red);
vpo[0].set_style(Line_style(Line_style::solid,3));
win.attach(vpo[0]);
for(int i=1; i<vpo.size(); i++) {
vpo[i].set_color(Color::blue);
win.attach(vpo[i]);
}
win.attach(b_t);
win.wait_for_button();
return 0;
}
//*****************************
catch(exception& e) {
cerr << e.what() << "\n\a";
return 0;
}
And errors are:
*Error 9 error C2664: 'void std::vector<_Ty>::push_back(Graph_lib::Polygon &&)' : cannot convert parameter 1 from 'Graph_lib::Polygon *' to 'Graph_lib::Polygon &&' c:\users\cs\documents\visual studio 2012\projects\test_1\test_1\test_1.cpp 53
14 IntelliSense: no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=Graph_lib::Polygon, _Alloc=std::allocator]" matches the argument list argument types are: (Graph_lib::Polygon ) object type is: std::vector> c:\Users\CS\Documents\Visual Studio 2012\Projects\test_1\test_1\test_1.cpp 53