You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++类构造函数数组默认参数定义问题求助

C++ OpenGL正多边形类默认参数编译错误解决

问题场景

在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;
            }
        }
};

遇到的编译错误

  1. 使用非静态成员default_center作为默认参数时:

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

  1. 未给centerIn设置默认参数时:

error: missing default argument on parameter 'centerIn'

  1. 尝试直接写double centerIn[2] = {0.0, 0.0}时,编译器将数组中的逗号识别为新参数分隔符,导致语法错误。

解决方法

方法1:将默认中心设为静态成员变量

C++不允许使用非静态成员变量作为函数参数的默认值,因此需要把default_center声明为静态成员,并在类外部定义:

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

class EquilateralShape {
    public:
        int sides;
        double center[2];
        double radius;
        
        // 声明静态默认中心
        static double default_center[2];

        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;
        }

        // 渲染函数保持不变(注意后续要修正角度转弧度问题)
        void render() {
            // ... 原始渲染代码
        }
};

// 在类外部定义静态成员
double EquilateralShape::default_center[2] = {0.0, 0.0};

方法2:改用std::array替代原生数组(更推荐)

原生数组作为函数参数会退化为指针,使用std::array可以保留数组的大小信息,语法更清晰,默认参数设置也更直观:

#include "../vendors/GLFW/glfw3.h"
#include <cmath>
#include <array> // 引入std::array头文件

class EquilateralShape {
    public:
        int sides;
        std::array<double, 2> center;
        double radius;
        
        // 静态constexpr默认中心,支持编译期初始化
        static constexpr std::array<double, 2> default_center = {0.0, 0.0};

        // 使用初始化列表初始化成员,更高效
        EquilateralShape(int sidesIn = 3, const std::array<double, 2>& centerIn = default_center, double radiusIn = 1.0)
            : sides(sidesIn), center(centerIn), radius(radiusIn) {}

        void render() {
            std::array<double, 2> previous_vertex = {center[0] + radius, center[1]};
            for(int i = 1; i < sides + 1; i++) {
                // 计算角度(转换为弧度,因为sin/cos接受弧度值)
                double angle_deg = (360.0 * i) / sides;
                double angle_rad = angle_deg * M_PI / 180.0;

                // 直接用三角函数计算顶点,无需手动判断正负
                double x_coord = center[0] + radius * cos(angle_rad);
                double y_coord = center[1] + radius * sin(angle_rad);

                // 渲染三角形
                glBegin(GL_TRIANGLES);
                glVertex2d(x_coord, y_coord);
                glVertex2d(previous_vertex[0], previous_vertex[1]);
                glVertex2d(center[0], center[1]);
                glEnd();

                previous_vertex = {x_coord, y_coord};
            }
        }
};

关键额外修正

原始代码中sin()和cos()函数的参数是角度值,但这两个函数实际接受的是弧度值,会导致顶点位置计算错误。上述方法2中已修正为角度转弧度的计算,确保顶点位置正确。


内容的提问来源于stack exchange,提问作者Salty4141

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 10:14:54