gpt4 book ai didi

c++ - 问题覆盖绘制曲线的 c++ 类

转载 作者:行者123 更新时间:2023-11-30 01:46:36 25 4
gpt4 key购买 nike

我正在尝试实现一个程序,当用户点击屏幕时,该程序会点对点绘制线条。我有一个 Polyline 类,它是 Freeform 类的子类,而 Freeform 类又是 Curve 类的子类。 Curve 父类(super class)的 draw 方法通常调用 getPoint,它将在该特定点获得曲线的加权点。但是,对于 Polyline 的 draw 方法,我试图覆盖曲线类以仅获取用户单击的点(如您所见,Polyline 类的 draw 方法从不调用 getPoint)。但是,当我调试代码时,我发现在尝试绘制多段线时仍在调用 getPoint。有什么建议吗?

class Curve {

public:
virtual float2 getPoint(float t)=0;

void draw(){

glBegin(GL_LINE_STRIP);
for (float i = 0; i < 1; i+=.01) {
float2 point = getPoint(i);
float x = point.x;
float y = point.y;
glVertex2d(x, y);

}
glEnd();
};
};

class Freeform : public Curve
{
protected:
std::vector<float2> controlPoints;

public:
virtual float2 getPoint(float t)=0;
virtual void addControlPoint(float2 p)
{
controlPoints.push_back(p);
}
void drawControlPoints(){
glBegin(GL_POINTS);
for (float i = 0; i < controlPoints.size(); i++) {
float2 point = controlPoints.at(i);
float x = point.x;
float y = point.y;
glVertex2d(x, y);

}
glEnd();// draw points at control points
}
};

class Polyline : public Freeform {
public:
float2 getPoint(float t) {
return float2(0.0, 0.0);
}
//we add a control point (in this case, control point is where mouse is clicked)
void addControlPoint(float2 p)
{
controlPoints.push_back(p);
}
//trying to override Curve draw method
void draw(){
glBegin(GL_LINE_STRIP);
for (float i = 0; i < controlPoints.size(); i++) {
float2 point = controlPoints.at(i);
float x = point.x;
float y = point.y;
glVertex2d(x, y);
}
glEnd();
};
};

最佳答案

//trying to override Curve draw method

上面的评论几乎说明了一切;您正在“尝试”覆盖void Curve::draw (),但由于成员函数之前未声明,因此未能成功虚拟


解决方案

  • void Curve::draw () 声明为 virtual
  • 如果或稍后,查看(并使用)关键字 override .

关于c++ - 问题覆盖绘制曲线的 c++ 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32920470/

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