push_back(新三角)

时间:2014-10-11 07:10:50

标签: c++ visual-c++ fltk

在FLTK的C ++中,为了定义一个圆圈,我们使用了这样的代码:

Circle c(Point(x,y), r);

我们可以使用vector_ref put并将它们保存在矢量中,例如:

Vector_ref<Circle> vc;
vc.push_back(new Circle(Point(x,y),r));

好的,那些关于Circle并没有问题,直到现在! 在代码中使用三角形可以这样定义:

Graph_lib::polygon poly;
poly.add(Point(x1,y1),r1);
poly.add(Point(x2,y2),r2);
poly.add(Point(x3,y3),r3);

这是一个保存它们的向量:

Vector_ref<Graph_lib::Polygon> vp;

问题是,如何使用圈子中的triangles/polygons关键字将new保存/放入该vp向量中?

我用作第12号练习答案的代码(here)   是这样的:

/* 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;
}

错误是:

*错误9错误C2664:'void std :: vector&lt; _Ty&gt; :: push_back(Graph_lib :: Polygon&amp;&amp;)':无法将参数1从'Graph_lib :: Polygon *'转换为'Graph_lib ::多边形&amp;&amp;' c:\ users \ cs \ documents \ visual studio 2012 \ projects \ test_1 \ test_1 \ test_1.cpp 53

14智能感知:没有重载函数的实例“std :: vector&lt; _Ty,_Alloc&gt; :: push_back [with _Ty = Graph_lib :: Polygon,_Alloc = std :: allocator]”匹配参数列表             参数类型是:(Graph_lib :: Polygon )             对象类型是:std :: vector&gt; c:\ Users \ CS \ Documents \ Visual Studio 2012 \ Projects \ test_1 \ test_1 \ test_1.cpp 53

1 个答案:

答案 0 :(得分:2)

对于Vector_ref<polygon>(使用new

根据我对Vector_ref的理解,这只是一个带有指针和代码的std::vector,用于清理最后动态分配的内存。这意味着您必须使用new来分配多边形。因此,如果您必须使用new,则可以先创建多边形(使用new),然后按下它:

// Create your new polygon
polygon* poly = new polygon;
poly->add(Point(...));
poly->add(...);
poly->add(...);

// Push that polygon into the vector
vp.push_back(poly);

对于std::vector<polygon*>(取地址)

如果多边形没有超出范围(即你不离开函数或传递}),你可以获取多边形的地址:

// Create polygon
polygon poly;
poly.add(...); // by 3

// Add the polygon to the vector
vp.push_back(&poly);

对于std::vector<polygon>(制作副本)

如果向量不是指针数组,std::vector<polygon>(但不适用于std::vector<polygon*>Vector_ref<polygon>),则不应使用new&。相反,如果你只使用矢量的push_back和多边形,那么多边形将被复制到矢量中。

// Create your new polygon
polygon poly;
poly.add(...); // by 3

// Copy the polygon into the vector
vp.push_back(poly);