定义类构造函数的数组默认参数 C++

问题描述 投票:0回答:1

我正在尝试创建一个类来在 Mac 上使用 OpenGL 创建正常形状(等边形状)。

给定的参数是中心坐标(

double center[2]
)、从中心到顶点的半径(
double radius
)和边数(
int sides
):

#include "../vendors/GLFW/glfw3.h"
#include <cmath>

class EquilateralShape {
    public:
        int sides;
        double center[2];
        double radius;
        
        double default_center[2] = {0.0, 0.0};

        EquilateralShape(int sidesIn = 3, double centerIn[2] = default_center, double radiusIn = 1.0) {
            sides = sidesIn;
            center[0] = centerIn[0];
            center[1] = centerIn[1];
            radius = radiusIn;
        }

        // rendering the shape
        void render() {
            // calculating vertices of the polygon
            double previous_vertex[2] = {center[0] + radius, center[1]};
            for(int i = 1; i < sides + 1; i++) {
                // determining if angle  is greater than 90, 180, 270, or 360
                double angle = (360*i)/sides;
                bool x_neg = false;
                bool y_neg = false;
                if (angle > 270) {
                    angle = 360 - angle;
                    y_neg = true;
                } else if (angle > 180) {
                    angle = -1 * (180-angle);
                    y_neg = true;
                    x_neg = true;
                } else if (angle > 90) {
                    angle = 180 - angle;
                    x_neg = true;
                }

                // calculating vertices
                double y_coord = radius * sin(angle);
                double x_coord = radius - radius * cos(angle);

                // updating coordinates to match unit circle group
                if (x_neg == true) {
                    x_coord *= -1;
                }
                if (y_neg == true) {
                    y_coord *= -1;
                }

                // rendering triangle
                glBegin(GL_TRIANGLES);
                glVertex2d(x_coord, y_coord);
                glVertex2d(previous_vertex[0], previous_vertex[1]);
                glVertex2d(center[0], center[1]);
                glEnd();

                // updating previous vertex
                previous_vertex[0] = x_coord;
                previous_vertex[1] = y_coord;
            }
        }
};

...但是当我编译时,我被告知:

error: invalid use of non-static data member 'default_center'

这是我为center传递的默认参数。

最初我尝试不包含默认参数,但这也给了我一个错误:

error: missing default argument on parameter 'centerIn'

我还尝试以数组形式传递默认参数,格式为

double centerIn[2] = {0.0, 0.0}
,但编译器告诉我它将数组条目之间的逗号解释为新参数。

c++ macos opengl glfw
1个回答
0
投票

参数默认值在调用站点被替换。因此,您不能使用非

static
类成员作为参数的默认值。

让班级成员成为

static
const
然后就可以工作了,例如:

class EquilateralShape {
    public:
        static const double default_center[2];

        EquilateralShape(int sidesIn = 3, const double centerIn[2] = default_center, double radiusIn = 1.0) {
            ...
        }
};
// and then in some .cpp file:
const double EquilateralShape::default_center[2] = {0.0, 0.0};

或者,在 C++17 及更高版本中:

class EquilateralShape {
    public:
        static constexpr double default_center[2] = {0.0, 0.0};

        EquilateralShape(int sidesIn = 3, const double centerIn[2] = default_center, double radiusIn = 1.0) {
            ...
        }
};
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.