ylliX - Online Advertising Network
How to build GenAI mock server?

Defining Array Default Parameters of a Class Constructor C++


I’m trying to make a class to create a normal shape (equilateral shape) using OpenGL on Mac. The given parameters are a center coordinate (double center[2]), a radius from center to a vertex (double radius), and a number of sides (int sides).
Here’s the code:

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

The issue I’ve been getting is that when I compile, I’m told:
error: invalid use of non-static data member 'default_center'
This refers to the defaults parameter I passed for center.

Originally I tried not including a default parameter, but that also gave me an error:
error: missing default argument on parameter 'centerIn'

I also tried passing the default parameter as an array, in the form double centerIn[2] = {0.0, 0.0} but the compiler told me it interpreted the comma between the array entries as a new argument.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *