作者remember11 (Mr.Darcy)
看板C_and_CPP
标题[问题] typedef enum 的 void* 用法请益
时间Mon Aug 26 19:01:39 2013
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
G++
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
问题(Question):
小弟将一段 .c 的code贴到 .cpp上,用 g++ compiler
产上下述错误结果 (错误的程式在程式码内两行标记处 //error)
不知道为什麽c++ 这样用 void* 会有错
烦请各位大大指点,该如何改正?
谢谢
错误结果(Wrong Output):
player.cpp:25: error: invalid cast from type 'void*' to type 'eGOPLAYER_STATE'
player.cpp:27: error: invalid cast from type 'void*' to type 'eGOPLAYER_STATE'
程式码(Code):(请善用置底文网页, 记得排版)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <malloc.h>
#include <unistd.h>
typedef enum
{
eGOPLAYER_STATE_STOP,
eGOPLAYER_STATE_PLAY,
eGOPLAYER_STATE_PAUSE
}eGOPLAYER_STATE;
typedef void(* GOPLAYER_STREAM_CALLBACK)(eGOPLAYER_CALLBACK_TYPE type, void
*data);
void cb_func(eGOPLAYER_CALLBACK_TYPE type, void *data)
{
switch (type)
{
case eGOPLAYER_CBT_STATE_CHANGE:
{
if((eGOPLAYER_STATE)data == eGOPLAYER_STATE_PAUSE) //error
printf("[sample]state : pause\n");
else if ((eGOPLAYER_STATE)data == eGOPLAYER_STATE_PLAY){ //error
printf("[sample]state : play\n");
}
else
printf("[sample]state : stop\n");
}
break;
default:
break;
}
}
int player_open_mms(char *uri)
{
player_open(cb_func);
player_set_source_uri(uri, 0);
player_close();
return 0;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.120.184.171
※ remember11:转录至看板 Programming 08/26 19:03
1F:推 purpose:if ( (int)data == eGOPLAYER_STATE_PAUSE ) 08/26 19:19
2F:→ purpose:好符合 C_AND_CPP 版时事的问题 08/26 19:20
谢谢
不过这样改似乎就失去callback的意义喔?
3F:推 CaptainH:我记得 C++ 不能(直接) cast 成 enum 08/26 19:31
4F:→ Feis:void * 转 int 可能不会过 08/26 22:19
5F:推 cackerDT:void *这是指标 表示data是记忆体位址 08/27 00:56
6F:→ cackerDT:eGOPLAYER_STATE 改成 eGOPLAYER_STATE *试看看 08/27 00:57
7F:→ cackerDT:对了 if比较内容应该也要改成*data 试看看喔 08/27 01:05
谢谢
不过两个方法都试过
compiler一样有error
所以C++没办法这样写?
※ 编辑: remember11 来自: 60.251.192.93 (08/27 13:47)