无法在列表中使用push_back元素

时间:2011-10-09 20:28:38

标签: c++ list

我的代码遇到了一个非常特殊的问题, 我无法将元素推入列表。

我正在尝试实施扫描填充算法。 我想要在屏幕上绘制的点列表,以便我可以检查扫描线是否与它们相交。 在我的Screen :: plot_pixel函数中,我将Point推入mapped_points列表。 但是当我遍历列表时,它是空的。(我正在使用友元函数迭代shape.cpp)

我尝试使用套装,但无济于事。我也附上了控制台输出。

plot_pixel被多次调用,我通过在那里添加一个print语句验证了这一点但是这些点拒绝被推入。 这是我的所有代码,point.h

#ifndef POINT_H
#define POINT_H
class Point
{
    float x_coordinate,y_coordinate;
public:
    Point(){}
    Point(float x, float y):x_coordinate(x),y_coordinate(y){}
    float get_x() const{return x_coordinate;}
    float get_y() const {return y_coordinate;}

    bool operator==(const Point rhs)const
    {
        if( ((int)x_coordinate == (int)rhs.get_x()) && ((int)y_coordinate == (int)rhs.get_y()) )
            return true;
        else return false;
    }

    bool operator<(const Point rhs)const
    {
        if((int)x_coordinate < (int)rhs.get_x())
            return true;
        else return false;
    }
};
#endif

screen.h

#ifndef SCREEN_H
#define SCREEN_H
#include<graphics.h>
#include "point.h"
#include<list>

class Shape;

class Screen
{
private:
    int max_x,max_y,grid_size;
    int delta_x,delta_y;
    int origin_x,origin_y;
public:
    std::list<Point> plotted_points;

    // Default Constructor to Initialize the screen with the grid
    Screen(int, int, int);

    //Method prototypes
    void plot_pixel(int,int,int);
    void dda_line(Point p1, Point p2);
    int get_max_x(){return max_x;}
    int get_max_y(){ return max_y;}
    friend void draw_shape(Screen,Shape);
    friend void fill_shape(Screen,Shape);    
};
#endif

screen.cpp

#include "screen.h"

void Screen::plot_pixel(int xcoord,int ycoord,int col=WHITE)
{
    int l,t,r,b;
    l = origin_x + xcoord * delta_x;
    r = l + delta_x;
    b = origin_y - ycoord * delta_y;
    t = b - delta_y;

    setcolor(col);
    bar(l,t,r,b);
    setcolor(WHITE);
    Point *tmp = new Point(xcoord,ycoord);

    //the culprit code
    plotted_points.push_back(*tmp);
    delete tmp;
}

void Screen::dda_line(Point p1, Point p2)
{
.....
    plot_pixel(x,y,0);
}

shape.h

#ifndef SHAPE_H
#define SHAPE_H

#include<list>
#include<iostream>
#include "point.h;

class Screen;

class Shape
{
    list<Point> figure;
public:
    Shape(list<Point> s);
    friend void draw_shape(Screen,Shape);
    friend void fill_shape(Screen,Shape);
};

#endif

shape.cpp

#include "shape.h"
#include "screen.h"

Shape::Shape(list<Point> s):figure(s){}
void fill_shape(Screen scr, Shape sh)
{
    list<Point>::iterator pit,vit;

    //HERE's Where I try iterating over the list and get nothing
    //to check if the plotted points and vertices are listed correctly
    cout<<"Plotted Points :\n";

    for( pit = scr.plotted_points.begin();pit != scr.plotted_points.end();pit++)
    {
        cout<<pit->get_x()<<" "<<pit->get_y()<<endl;
    }

    cout<<"Vertices :\n";

    for( vit = sh.figure.begin();vit != sh.figure.end();vit++)
    {
        cout<<vit->get_x()<<" "<<vit->get_y()<<endl;
    }
}

最后是我的司机

#include "screen.h"
#include "shape.h"

using namespace std;

int main()
{
    Screen s(641,641,32);
    Point p1(-10,-10),p2(-10,10),p3(10,10),p4(10,-10);
    list<Point> rectangle_points;

    //to construct the rectangle
    rectangle_points.push_back(p1);
    rectangle_points.push_back(p2);
    rectangle_points.push_back(p3);
    rectangle_points.push_back(p4);
    Shape rectangle(rectangle_points);

    draw_shape(s,rectangle);
    fill_shape(s,rectangle);

    getch();

    return 0;
}

这是输出

Plotted Points :
Vertices :
-10 -10
-10 10
10 10
10 -10

1 个答案:

答案 0 :(得分:10)

您正在将Screen对象传递给您的函数,但它们按值获取Screen对象。因此,他们会获取他们随后修改的对象的副本。调用draw_shape()后原始屏幕“s”保持不变。

您应该修改它们,以便它们引用Screen对象,例如:

void draw_shape(Screen& scr, Shape shp)