作者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
推 purpose:if ( (int)data == eGOPLAYER_STATE_PAUSE ) 08/26 19:19
→ purpose:好符合 C_AND_CPP 版時事的問題 08/26 19:20
謝謝
不過這樣改似乎就失去callback的意義喔?
推 CaptainH:我記得 C++ 不能(直接) cast 成 enum 08/26 19:31
→ Feis:void * 轉 int 可能不會過 08/26 22:19
推 cackerDT:void *這是指標 表示data是記憶體位址 08/27 00:56
→ cackerDT:eGOPLAYER_STATE 改成 eGOPLAYER_STATE *試看看 08/27 00:57
→ cackerDT:對了 if比較內容應該也要改成*data 試看看喔 08/27 01:05
謝謝
不過兩個方法都試過
compiler一樣有error
所以C++沒辦法這樣寫?
※ 編輯: remember11 來自: 60.251.192.93 (08/27 13:47)