Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;类成员函数不修改成员_C++_Class_C++11 - Fatal编程技术网

C++ C++;类成员函数不修改成员

C++ C++;类成员函数不修改成员,c++,class,c++11,C++,Class,C++11,我从一个基本的Shape类创建了一个派生的Octagon类(由Stroustrup编写)。我正在使用编程原理和实践。构造函数和其他方法似乎很好,只是调用修改类成员的函数不会改变任何东西 这是我编写的派生类(在基类接口之后): 我不知道这是否必要,但这是Stroustrup使用fltkgui库设计的基本Shape类 class Shape { // deals with color and style, and holds sequence of lines public:

我从一个基本的
Shape
类创建了一个派生的
Octagon
类(由Stroustrup编写)。我正在使用编程原理和实践。构造函数和其他方法似乎很好,只是调用修改类成员的函数不会改变任何东西

这是我编写的派生类(在基类接口之后):

我不知道这是否必要,但这是Stroustrup使用fltkgui库设计的基本
Shape

class Shape  {        // deals with color and style, and holds sequence of lines 
public:
    void draw() const;                 // deal with color and draw lines
    virtual void move(int dx, int dy); // move the shape +=dx and +=dy

    void set_color(Color col) { lcolor = col; }
    Color color() const { return lcolor; }
    void set_style(Line_style sty) { ls = sty; }
    Line_style style() const { return ls; }
    void set_fill_color(Color col) { fcolor = col; }
    Color fill_color() const { return fcolor; }

    Point point(int i) const { return points[i]; } // read only access to points
    int number_of_points() const { return int(points.size()); }

    virtual ~Shape() { }
protected:
    Shape();    
    virtual void draw_lines() const;   // draw the appropriate lines
    void add(Point p);                 // add p to points
    void set_point(int i,Point p);     // points[i]=p;
private:
    vector<Point> points;              // not used by all shapes
    Color lcolor;                      // color for lines and characters
    Line_style ls; 
    Color fcolor;                      // fill color

    Shape(const Shape&);               // prevent copying
    Shape& operator=(const Shape&);
};
class Shape{//处理颜色和样式,并保存行序列
公众:
void draw()const;//处理颜色和绘制线条
虚拟空心移动(int-dx,int-dy);//移动形状+=dx和+=dy
void set_color(color col){lcolor=col;}
Color Color()常量{return lcolor;}
空集样式(线样式sty){ls=sty;}
Line_style()常量{return ls;}
空集\填充\颜色(颜色列){fcolor=col;}
颜色填充颜色()常量{return fcolor;}
Point Point(int i)const{return points[i];}//对点的只读访问
int number_of_points()常量{return int(points.size());}
虚拟~Shape(){}
受保护的:
形状();
虚空绘制线()常量;//绘制适当的线
void add(点p);//将p添加到点
void set_point(int i,p点);//points[i]=p;
私人:
向量点;//并非所有形状都使用
Color lcolor;//行和字符的颜色
线条式ls;
颜色fcolor;//填充颜色
Shape(const Shape&);//防止复制
形状和运算符=(常量形状和);
};

顺便说一句,
draw\u lines()
由显示引擎调用的
draw()
调用。(我正在一章一章地阅读这本书,所以我还没有到达他详细讨论这一点的章节)。

您只在构造函数中使用
s\u l
。您可以稍后将其设置为其他值,但工作已经完成。是否要删除所有点并重新计算它们?如果是这样,你需要这样做,它不会神奇地发生。

显然,在你修改了s_l之后,你永远不会重新计算点。一个简单的解决方案是将点创建代码从构造函数移到一个单独的方法中,该方法从构造函数和set_端调用(后者也应首先删除以前存在的点)。或者,如果足够简单,您也可以从set_side()修改现有点。

问题在于形状点是在构造函数中计算的,并且永远不会重置。当为
s_l
设置新值时,马已经离开了马厩:形状的点不会在心中以
s_l
的新值重建

更改setter以将点重置为其新值以使其正常工作:

void set_side(int x) {
     s_l = x;
     int pos = 0;
     for (double ang = 22.5 ; ang <= 337.5 ; ang += 45) {
         set_point(pos++, Point {
             int(centre.x + rad(s_l) * cos(ang*PI / 180))
         ,   int(centre.y - rad(s_l) * sin(ang*PI / 180))
         });
    };
}
void set_侧(int x){
s_l=x;
int pos=0;
对于(双ang=22.5;ang
class Shape  {        // deals with color and style, and holds sequence of lines 
public:
    void draw() const;                 // deal with color and draw lines
    virtual void move(int dx, int dy); // move the shape +=dx and +=dy

    void set_color(Color col) { lcolor = col; }
    Color color() const { return lcolor; }
    void set_style(Line_style sty) { ls = sty; }
    Line_style style() const { return ls; }
    void set_fill_color(Color col) { fcolor = col; }
    Color fill_color() const { return fcolor; }

    Point point(int i) const { return points[i]; } // read only access to points
    int number_of_points() const { return int(points.size()); }

    virtual ~Shape() { }
protected:
    Shape();    
    virtual void draw_lines() const;   // draw the appropriate lines
    void add(Point p);                 // add p to points
    void set_point(int i,Point p);     // points[i]=p;
private:
    vector<Point> points;              // not used by all shapes
    Color lcolor;                      // color for lines and characters
    Line_style ls; 
    Color fcolor;                      // fill color

    Shape(const Shape&);               // prevent copying
    Shape& operator=(const Shape&);
};
void set_side(int x) {
     s_l = x;
     int pos = 0;
     for (double ang = 22.5 ; ang <= 337.5 ; ang += 45) {
         set_point(pos++, Point {
             int(centre.x + rad(s_l) * cos(ang*PI / 180))
         ,   int(centre.y - rad(s_l) * sin(ang*PI / 180))
         });
    };
}