且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将成员函数作为函数指针传递

更新时间:2023-11-09 23:26:10



" cps" < CS ****** @ qub.ac.uk>在消息中写道

news:11 ********************* @ i39g2000cwa.googlegro ups.com ...

"cps" <cs******@qub.ac.uk> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...


我是C程序员,迈出了进入C ++世界的第一步。

我正在使用GLUT开发C ++ 3D图形应用程序
(用C语言编写的OpenGL实用工具包)用于GUI组件。

应用程序是围绕世界组件构建的。包含GUI对象的对象,该对象是GLUT的C ++包装器。我想要做的是将一个
指针传递给World类中的成员函数,以便在GUI
类中运行。

以下是我的函数d想通过:

void World :: Display()
{
/ *清除所有像素* /
glClear(GL_COLOR_BUFFER_BIT);


无效的World :: Keyboard(unsigned char key,int x,int y)
{
cout< &LT; key<< endl;

处理键盘......

我想将这些函数传递给GUI对象的构造函数
如下:

p_GUI =新的GUI(显示,键盘);


我在任何地方都没有看到你对GUI :: GUI的定义。你有什么构造函数

parms?

我收到以下错误:

World.cpp(16):错误C2664:' 'GUI :: GUI(void(__ cdecl *)(void),void
(__ cdecl *)(unsigned char,int,int))'':无法转换参数1来自
''void(void )''to''void(__ cdecl *)(void)''
范围内此名称的所有函数都不匹配目标


我理解(__cdecl *)是一个召集会议。我无法弄明白的是如何修复语法以便代码编译。

任何对此的帮助(特别是一个简单的例子)都会非常多。
赞。

干杯,

Chris
Hi,

I''m a C programmer taking my first steps into the world of C++.

I''m currently developing a C++ 3D graphics application using GLUT
(OpenGL Utility Toolkit written in C) for the GUI components.

The application is built around a "World" object that contains a "GUI"
object that is a C++ wrapper around GLUT. What I''d like to do is pass a
pointer to a member function in the World class to function in the GUI
class.

Here are the functions I''d like to pass:
void World::Display()
{
/* clear all pixels */
glClear(GL_COLOR_BUFFER_BIT);

Do OpenGL stuff...
}

void World::Keyboard(unsigned char key, int x, int y)
{
cout << key << endl;

Handle keyboard...
}
I''d like to pass these functions to the constructor of the GUI object
as follows:
p_GUI = new GUI(Display, Keyboard);
I don''t see your definition of GUI::GUI anywhere. What are your constructor
parms?
I''m getting the following error:

World.cpp(16) : error C2664: ''GUI::GUI(void (__cdecl *)(void),void
(__cdecl *)(unsigned char,int,int))'' : cannot convert parameter 1 from
''void (void)'' to ''void (__cdecl *)(void)''
None of the functions with this name in scope match the target
type

I understand that (__cdecl *) is a calling convention. What I can''t
figure out is how to fix the syntax so that the code compiles.

Any help with this (especially a simple example) would be very much
appreciated.

Cheers,

Chris



这里是:

// GUI.h


#ifndef GUI_H

#define GUI_H


类GUI

{

public:

GUI();

GUI(void(* displayFunctionPtr)(),void

(* keyboardFunctionPtr)(unsigned char key,int x,int y));

~GUI();


private:
};


#endif

GUI :: GUI(void(* displayFunctionPtr)(),void

(* keyboardFunctionPtr)(unsigned char key,int x,int y))

{

int argc = 0;

char * argv [30];


glutInit(& argc,argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(250,250);

glutInitWindo wPosition(100,100);

glutCreateWindow(" Test");

glClearColor(0.0,0.0,0.0,0.0);

glShadeModel(GL_FLAT);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(0.0,1.0,0.0,1.0,-1.0 ,1.0);

glutDisplayFunc(displayFunctionPtr);

glutIdleFunc(displayFunctionPtr);

glutKeyboardFunc(keyboardFunctionPtr);

glutMainLoop();

}

Here they are:
// GUI.h

#ifndef GUI_H
#define GUI_H

class GUI
{
public:
GUI();
GUI(void (*displayFunctionPtr)(), void
(*keyboardFunctionPtr)(unsigned char key, int x, int y));
~GUI();

private:
};

#endif
GUI::GUI(void (*displayFunctionPtr)(), void
(*keyboardFunctionPtr)(unsigned char key, int x, int y))
{
int argc = 0;
char* argv[30];

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow("Test");
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glutDisplayFunc(displayFunctionPtr);
glutIdleFunc(displayFunctionPtr);
glutKeyboardFunc(keyboardFunctionPtr);
glutMainLoop();
}


" cps" &LT; CS ****** @ qub.ac.uk&GT;在消息中写道

news:11 ********************* @ i39g2000cwa.googlegro ups.com ...
"cps" <cs******@qub.ac.uk> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...


我是C程序员,迈出了迈向C ++的第一步。
Hi,

I''m a C programmer taking my first steps into the world of C++.




请参阅FAQ:
http:// www。 parashift.com/c++-faq-lit...o-members.html


- Paul



Please see the FAQ:
http://www.parashift.com/c++-faq-lit...o-members.html

- Paul

>