找回密码
 注册
查看: 974|回复: 12

C++里一个函数指针问题

[复制链接]
发表于 2005-8-5 18:21:46 | 显示全部楼层 |阅读模式
[code:1]
#include <SDL.h>
#include <SDL_thread.h>

class test
{
    public:
        test();
        int callback(void *data);
        SDL_Thread *thread;
};

int test::callback(void *data)
{
    return 0;
}

test::test()
{
    thread = SDL_CreateThread(callback,NULL);
}
main()
{
}

[/code:1]

错误信息:
[root@localhost SDL]# g++ a.cpp `sdl-config --libs --cflags`
a.cpp: In constructor 'test::test()':
a.cpp:19: error: argument of type 'int (test:(void*)' does not match 'int (*)(void*)'

怎么让一个成员函数称为callback函数?
 楼主| 发表于 2005-8-5 18:25:03 | 显示全部楼层
刚学C++,同志们多多支持啊
回复

使用道具 举报

发表于 2005-8-5 19:08:06 | 显示全部楼层
不会

留名关注
回复

使用道具 举报

发表于 2005-8-5 19:12:53 | 显示全部楼层
thread = SDL_CreateThread(callback,NULL);

觉得这句应该再分一个类来写
回复

使用道具 举报

发表于 2005-8-5 19:14:23 | 显示全部楼层
试试这个

[code:1]#include <SDL.h>
#include <SDL_thread.h>

class test
{
            public:
                        test();
                                ~test();
                        SDL_Thread *thread;
};

int callback(void *data)
{
                return 0;
}

test::test()
{
                thread = SDL_CreateThread(callback,NULL);
}

test::~test(){
//      delete thread;
}

main()
{
}

[/code:1]
回复

使用道具 举报

 楼主| 发表于 2005-8-5 19:21:57 | 显示全部楼层
这肯定可以,我意思是成员函数
回复

使用道具 举报

 楼主| 发表于 2005-8-5 19:23:07 | 显示全部楼层
因为callback要操作成员变量
回复

使用道具 举报

发表于 2005-8-5 19:34:13 | 显示全部楼层
哦,

那就不知了
回复

使用道具 举报

发表于 2005-8-6 00:58:21 | 显示全部楼层
这个……mozilla那个代码里的函数是成员函数,必须用成员函数指针来操作,成员函数指针和那种普通函数指针是有区别的……你还是写个全局函数封装一下吧……
回复

使用道具 举报

发表于 2005-8-6 09:16:31 | 显示全部楼层
我不知道SDL_CreateThread是怎么处理得 但是我在pthread的createthread时 是把callback做成static  然后把整个类指针this 作为线程的参数传递过去 在callback里变回来

[code:1]#include <SDL.h>
#include <SDL_thread.h>

class test
{
            public:
                        test();
                                ~test();
                        SDL_Thread *thread;
                        static int callback(void *data);
};

int test::callback(void *data)
{
               test* the_test=(test*)data;
               //  接着就可以用 the_test->  来操作了
}

test::test()
{
                //我猜SDL_CreateThread 的第二个参数是用来传递给线程的吧
                thread = SDL_CreateThread(callback,(void*)this );
}

test::~test(){
//      delete thread;
}

main()
{
}

[/code:1]
回复

使用道具 举报

 楼主| 发表于 2005-8-6 18:43:50 | 显示全部楼层
友元和静态应该都可以,晚上试试。
回复

使用道具 举报

 楼主| 发表于 2005-8-7 17:27:35 | 显示全部楼层
[code:1]
#include <pthread.h>

class test
{
    public:
        test();
        friend  void* callback(void *data);
        pthread_t thread;
};

void *callback(void *data)
{
}

test::test()
{
        pthread_create(&thread,NULL,callback,NULL);
}
main()
{

}
[/code:1]
这个可以,但把friend改成static编译时就报错
/cygdrive/c/DOCUME~1/GUOYON~1/LOCALS~1/Temp/ccIyEo2w.o(.text+0x1:a.cc: undefined reference to `test::callback(void*)'
/cygdrive/c/DOCUME~1/GUOYON~1/LOCALS~1/Temp/ccIyEo2w.o(.text+0x44):a.cc: undefined reference to `test::callback(void*)'
collect2: ld returned 1 exit status
回复

使用道具 举报

 楼主| 发表于 2005-8-7 17:28:47 | 显示全部楼层
sorry,定义改成void * test::callback(void *data) {}就行了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2025-2-8 03:21 , Processed in 0.031279 second(s), 15 queries .

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表