c++ - gluTessCallback 错误 C2440

标签 c++ opengl visual-studio-2012 compiler-errors tessellation

我正在尝试使用函数 gluTessCallback 但我收到 C2440 错误。我不知道为什么。

代码如下:

#define callback void(CALLBACK*)()

template<typename T>
class Tessellation
{
private:
    GLUtesselator *pTess;

    void CALLBACK tessError(GLenum error)
    {
        sendErrorMessage((char *)gluErrorString(error), true);
    }


 public:

    void Triangulation3D(T* & point, short numOfPoints)
    {
        pTess = gluNewTess();

        gluTessCallback(pTess, GLU_TESS_ERROR,      (callback)tessError);
    }
};

错误在 gluTessCallback 函数上:

error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'void (__stdcall *)(void)'

为什么会出现这个编译错误?

最佳答案

错误编号 C2440在 Visual Studio 上是类型转换错误。

您的代码中的问题是您试图将类方法 Tessellation::tessError() 作为函数指针传递给 gluTessCallback(),它需要一个指针到全局 C 风格函数。

类方法与自由/全局函数非常不同,您不能将它作为简单的函数指针传递,因为它每次都需要一个对象,this 指针。

您的问题的解决方案是将 tessError() 声明为静态方法,使其与类内范围内的自由/全局函数有效地相同,如下所示:

template<typename T>
class Tessellation
{
private:

    static void CALLBACK tessError(GLenum error)
    {
        sendErrorMessage((char *)gluErrorString(error), true);
    }
    ...

并将其传递给 gluTessCallback():

gluTessCallback(pTess, GLU_TESS_ERROR, (callback)&Tessellation<T>::tessError);

这种方法的唯一缺点是静态 tessError() 不能再访问类变量。这对您来说似乎不是问题,因为它现在没有这样做,看来。

关于c++ - gluTessCallback 错误 C2440,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25179786/

相关文章:

c++ - MSVC : explicit template instantiation fails while implicit instantiation succeeds

c++ - 取消WIN32线程池中计划的work/io/timer项

windows-8 - Visual Studio 11 ARM 项目生成器失败

entity-framework - Entity Framework 代码首先不使用 VS 2012 创建表

c++ - 在 Visual Studio 中将二进制文件添加到资源

c++ - CUDA 7.0,cuSolver 示例 : unresolved inclusion of cudense. h

c++ - 错误代码 C2676 二进制 '==' : 'std::pair<std::string,int>' does not define this operator or a conversion to a type acceptable to the predefined operato

c++ - 读取文件,损坏的数据

c++ - 在 OpenGL 中将纹理映射到球体时出现接缝问题

opengl - 如何使用 OpenGL 将 RGBA 转换为 NV12?