簡體   English   中英

在Visual C ++中使用回調函數指針進行編譯

[英]Compiling with Callback Function Pointers in Visual C++

我正在按照http://noobtuts.com/cpp/2d-pong-game提供的說明,使用名為freeglut的API制作一個簡單的乒乓游戲。 API中包含兩個函數,這些函數需要一個指向返回void的回調的指針,而我已在main函數下定義了該回調。 但是,在編譯時,我收到錯誤消息C2065,指出所提供的回調參數是未聲明的標識符。

當我將鼠標懸停在智能感知上時,這是它們提供的功能的定義。

void__stdcall glutDisplayFunc(void(* callback)())

void__stdcall glutTimerFunc(unsigned int time,void(* callback)(int),int value)

這是我的代碼:

#include "stdafx.h"
#include "freeglut.h"

#include <string>
#include <windows.h>
#include <iostream>
#include <conio.h>
#include <sstream>
#include <math.h>
#include <gl\GL.h>
#include <gl\GLU.h>

#pragma comment (lib, "OpenGL32.lib")

//window size and update rate (60 fps)
int width = 500;
int height = 200;
int interval = 1000 / 60;

int _tmain(int argc, char** argv)
{
    //initialize openg 1 (via glut)
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(width, height);
    glutCreateWindow("jackfrye.com Pong");

    //Register callback functions
    glutDisplayFunc(draw);
    glutTimerFunc(interval, update, 0);

    glutMainLoop();
    return 0;
}

void draw() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    //TODO: draw scene

    //swap buffers (has to be done at the end);
    glutSwapBuffers();
}

void update(int value) {
    //Call update() again in 'interval' milliseconds
    glutTimerFunc(interval, update, 0);

    //Redisplay frame
    glutPostRedisplay();
}

我嘗試將回調函數放在main函數中,但是我認為這是不正確的,並且仍然無法編譯。

您需要在使用回調函數之前聲明它們。 編譯器從頭到尾讀取源代碼。 _tmain函數中引用回調函數時,尚未看到它們。

明確聲明

void draw();
void update(int value);

在您的_tmain函數之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM