我在C++中有一个非常简单的openGL程序.我制作了一个Sphere对象,它只是绘制一个球体.我想要一个在main()中实例化的全局变量,即sphere = Sphere(半径等),然后在draw()中绘制,即sphere.draw(),但C++不会让我.或者,如果我在main()中引用了球体,那么我无法将它传递给draw函数,因为我自己没有定义绘制函数.这个伪代码可能会更好地解释它:
include "sphere.h"
Sphere sphere; <- can't do this for some reason
draw()
{
...
sphere.draw()
}
main()
{
glutDisplayFunc(draw)
sphere = Sphere(radius, etc)
}
Run Code Online (Sandbox Code Playgroud)
我确信这很简单,但谷歌找到答案很困难,相信我已经尝试过了.我知道使用全局变量是"坏"但似乎没有其他选择.我最终希望有另一个名为'world'的类,它包含对spheres和draw函数的引用,但另一个问题是我不知道如何将glutDisplayFunc重定向到类函数.我试过glutDisplayFunc(sphere.draw),显然这是一个可怕的错误.
编译器错误是:../ src/Cplanets.cpp:9:错误:没有匹配函数来调用'Sphere :: Sphere()'../src/Sphere.cpp:28:注意:候选者是:Sphere: :Sphere(std :: string,float,float,float)../src/coc.cpp:13:注意:Sphere :: Sphere(const Sphere&)
sphere类是:
/*
* 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);
}
};
Run Code Online (Sandbox Code Playgroud)
您正在处理两个构造函数调用:
Sphere sphere;
Run Code Online (Sandbox Code Playgroud)
这会尝试调用Sphere::Sphere()未声明的默认构造函数.
sphere = Sphere(radius, etc);
Run Code Online (Sandbox Code Playgroud)
这会调用构造函数接受两个参数,我认为这是唯一提供的参数.
像这样做:
include "sphere.h"
Sphere *sphere;
draw()
{
...
sphere->draw();
}
main()
{
sphere = new Sphere(radius, etc);
glutDisplayFunc(draw);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
963 次 |
| 最近记录: |