Initialize fixes of objects in C ++

1

I would like to do a C ++ fix of Point type, but I do not know how to do it.

This is my code:

#include <windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include "Punto.h"
#include "Linea.h"

Punto *vertices[2] = new Punto[2];

void init()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-200, 200, -200, 200);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    vertices[0] = new Punto(10.0, 25.0);

    Linea *l = new Linea(p1, p2);
    l->dibuja();


    delete l;

    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//SE INIZIALIZA LAS VARIABLES DE ENTORNO EN UN SOLO BUFFER Y COLOREA RGB
    glutInitWindowSize(400, 400);// se define el tamaño de la ventana
    glutInitWindowPosition(50,50); // se define las coordenadas inicilaes donde aparacera la ventana
    glutCreateWindow("Parcial_1");//crea una ventana y se le coloca una leyenda hola mundo
    init();                          //Funcion de servicio para inizializar parametros del ambiente grafico
    glutDisplayFunc(display);          // se define cula es la funcion que redibujara el ambiente grafico
    //glutIdleFunc(display);

    glutMainLoop();

    return 0;
}

Point Code.h

#ifndef PUNTO_H
#define PUNTO_H

class Punto
{
    public:
        double x, y;

    public:
        Punto(double x, double y);
        void dibuja(void);
        virtual ~Punto();

    protected:

    private:
};

#endif // PUNTO_H

Point Code.cpp

#include <windows.h>
#include <GL/glut.h>
#include "Punto.h"

Punto::Punto(double x, double y)
{
    this->x = x;
    this->y = y;
}

void Punto::dibuja(void)
{
    glBegin(GL_POINTS);
        glVertex2d(x, y);
    glEnd();
}

Punto::~Punto()
{
    //dtor
}

The error is here

Punto *vertices[2] = new Punto[2];

The error that marks is "no maching function for call 'Point :: Point ()'" And I would like to know if the initialization can be done as it is done in Java or C #

    
asked by EmmanCanVaz_95 18.09.2017 в 00:38
source

2 answers

0

The problem is here:

Punto *vertices[2] = new Punto[2];

For that to work, your class Punto needs from a constructor without parameters; however, your class does not have it .

You may know that C ++ generates builders for you ... if you do not generate any . Since you have a constructor Punto::Punto(double x, double y) , the compiler does not generate any, and shows you the indicated error.

The easiest solution is to create your own constructor without arguments:

  

Punto.h

#ifndef PUNTO_H
#define PUNTO_H

class Punto
{
    public:
        double x, y;

    public:
        Punto( ); //<- AÑADIMOS ESTO
        Punto(double x, double y);
        void dibuja(void);
        virtual ~Punto();

    protected:

    private:
};
  

Punto.cpp

#include <windows.h>
#include <GL/glut.h>
#include "Punto.h"

// AÑADIMOS ESTO. CONSTRUCTOR SIN PARÁMETROS
Punto::Punto( ) : x( 0.0 ), y( 0.0 ) {
}

Punto::Punto(double x, double y)
{
    this->x = x;
    this->y = y;
}

void Punto::dibuja(void)
{
    glBegin(GL_POINTS);
        glVertex2d(x, y);
    glEnd();
}

Punto::~Punto()
{
    //dtor
}

With that, we assign the values 0, 0 to your Point, when we create it without any parameter in the constructor.

    
answered by 18.09.2017 / 05:51
source
0

The problem is that the compiler waits for the operator () and not [] . Well, that's for starters, but the real solution was made by viewing an article that I'll leave the link here: link

Well, first remove the operator new followed by the class Point and leave it as is, an array of pointers.

Code

#include <windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include "Punto.h"
#include "Linea.h"

Punto *vertices[2];

void init()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-200, 200, -200, 200);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    vertices[0] = new Punto(10.0, 5.0);
    vertices[1] = new Punto(150, 150);

    Linea *l = new Linea(vertices);
    l->dibuja();


    delete l;

    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//SE INIZIALIZA LAS VARIABLES DE ENTORNO EN UN SOLO BUFFER Y COLOREA RGB
    glutInitWindowSize(400, 400);// se define el tamaño de la ventana
    glutInitWindowPosition(50,50); // se define las coordenadas inicilaes donde aparacera la ventana
    glutCreateWindow("Parcial_1");//crea una ventana y se le coloca una leyenda hola mundo
    init();                          //Funcion de servicio para inizializar parametros del ambiente grafico
    glutDisplayFunc(display);          // se define cula es la funcion que redibujara el ambiente grafico
    //glutIdleFunc(display);

    glutMainLoop();

    return 0;
}

I put the main.cppya code, which is the only file that I modified.

    
answered by 18.09.2017 в 01:11