eggbert
eggbert

Reputation: 3233

a very simple question about opengl, c++ and objects

I have a very simple openGL program in C++. I have made a Sphere object which simply draws a sphere. I want to have a global variable which gets instantiated in main(), i.e. sphere = Sphere(radius,etc), then gets drawn in draw(), i.e. sphere.draw(), but C++ won't let me. Alternatively, if I have the reference to the sphere in main(), then I can't pass it to the draw function because I haven't defined the draw function myself. This pseudocode might explain it a bit better:

include "sphere.h"
Sphere sphere;   <- can't do this for some reason

draw()
{
    ...
    sphere.draw()
}

main()
{
    glutDisplayFunc(draw)
    sphere = Sphere(radius, etc)
}    

I'm sure this is very simple, but its a difficult thing to Google for to find the answer, and believe me I have tried. I understand that using globals are 'bad' but there doesn't seem to be an alternative. I eventually want to have another class called 'world' which contains the references to spheres and a draw function, but yet another problem is that I don't know how to redirect the glutDisplayFunc to a class function. I tried glutDisplayFunc(sphere.draw), obviously this is hideously wrong.

The complier error is: ../src/Cplanets.cpp:9: error: no matching function for call to ‘Sphere::Sphere()’ ../src/Sphere.cpp:28: note: candidates are: Sphere::Sphere(std::string, float, float, float) ../src/Sphere.cpp:13: note: Sphere::Sphere(const Sphere&)

The sphere class is:

/*
 * Sphere.cpp
 *
 *  Created on: 3 Mar 2011
 *      Author: will
 */

#include <GL/glut.h>
#include <string>

using namespace std;

class Sphere {

public:

    string name;
    float radius;
    float orbit_distance;
    float orbit_time;

    static const int SLICES = 30;
    static const int STACKS = 30;

    GLUquadricObj *sphere;


    Sphere(string n, float r, float od, float ot)

    {

        name = n;
        radius = r;
        orbit_distance = od;
        orbit_time = ot;
        sphere = gluNewQuadric();

}

void draw()
{
    //gluSphere(self.sphere, self.radius, Sphere.SLICES, Sphere.STACKS)
    gluSphere(sphere, radius, SLICES, STACKS);
}

};

Upvotes: 1

Views: 1045

Answers (2)

Mahesh
Mahesh

Reputation: 34625

Sphere class has overridden the default constructor. If no constructor is specified in the class definition, the compiler automatically provides the default constructor (i.e., Sphere::Sphere()). Since the Sphere class has overridden it with a constructor taking four arguments, it's the job of class itself to specify the default one.

Upvotes: 1

Axel
Axel

Reputation: 14159

You are dealing with two constructor calls:

Sphere sphere;

This tries to call the default constructor Sphere::Sphere() which is not declared.

sphere = Sphere(radius, etc);

This calls the constructor taking two arguments, which I presume is the only one provided.

Do it like this:

include "sphere.h"
Sphere *sphere;

draw()
{
    ...
    sphere->draw();
}

main()
{
    sphere = new Sphere(radius, etc);
    glutDisplayFunc(draw);
}    

Upvotes: 5

Related Questions