C++ 无法向后推列表中的元素
我的代码遇到了一个非常奇怪的问题, 我无法将元素推入列表 我正在尝试实现一个扫描填充算法。 我想要在屏幕上绘制的点列表,以便检查扫描线是否与它们相交。 在my Screen::plot_pixel函数中,我将点推入plot_points列表。 但是当我遍历列表时,它是空的。(我使用一个friend函数在shape.cpp中迭代) 我也试过用一套,但没用。我也附加了控制台输出 plot_pixel被多次调用,我通过在那里添加一个print语句验证了这一点,但是点拒绝被推入。 这是我所有的代码,point.hC++ 无法向后推列表中的元素,c++,list,C++,List,我的代码遇到了一个非常奇怪的问题, 我无法将元素推入列表 我正在尝试实现一个扫描填充算法。 我想要在屏幕上绘制的点列表,以便检查扫描线是否与它们相交。 在my Screen::plot_pixel函数中,我将点推入plot_points列表。 但是当我遍历列表时,它是空的。(我使用一个friend函数在shape.cpp中迭代) 我也试过用一套,但没用。我也附加了控制台输出 plot_pixel被多次调用,我通过在那里添加一个print语句验证了这一点,但是点拒绝被推入。 这是我所有的代码,po
#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
形状.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
您正在将屏幕对象传递到函数中,但它们按值获取屏幕对象。因此,它们会获取对象的副本,然后对其进行修改。调用draw_shape()后,原始屏幕“s”保持不变 您可能应该修改它们,以便它们引用屏幕对象,例如:
void draw_shape(Screen& scr, Shape shp)
你能重新格式化你的代码,使它不是每一行都是空的吗?它使阅读更容易。尽量减少代码,噪音太大。@nikhil你花了这么多精力发布所有这些代码。你应该做的是发布一段相关的代码,我们可以用它来重现你的错误。您不能期望每个人都使用您的所有文件创建一个项目并运行它来查找您的问题。您有一个名为Ploted_points的全局列表,屏幕类中有一个名为Ploted_points的成员变量。我敢说你在代码的某个地方使用了错误的代码。@tinman:我只是在调试时把它放在那里,现在我已经删除了它,问题还是一样的。非常感谢你,错过了这个,我觉得自己像个十足的白痴。你是救命恩人。这真的很感激。
#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
#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
void draw_shape(Screen& scr, Shape shp)