改写别人的c代码为C++,但是如下语句在C++里面报错:
声明结构体:
typedef struct _HZPH {
    TCHAR szHZ[2*(MAX_PHRASE_LEN+1)];
    DWORD dwAttrib;
    struct _HZPH __based(lpMapFileBase) *lpNext;//
} HZPH, FAR *LPHZPH;typedef struct _KEYPH {
    SHORT wLen;
    BYTE abKey[MAX_PHRASE_LEN+1];
    HZPH __based(lpMapFileBase) *lpHZPH;//
    struct _KEYPH __based(lpMapFileBase) *lpNext;//
} KEYPH, FAR *LPKEYPH;
如下使用:
LPKEYPH lpKeyPH;
    LPHZPH lpHZPH;
.................
lpKeyPH->lpHZPH=(LPHZPH)((LPBYTE)lpMapFileBase+
dwBaseOffset+dwMapFileOffset);
提示如下错误:
c:\documents and settings\all users\documents\imedemo\dictctrl.cpp(164) :
error C2440: '=' : cannot convert from 'struct _HZPH *' to 'struct _HZPH
__based(lpMapFileBase) *'哪位遇到过啊?

解决方案 »

  1.   

    __based关键字
    该关键字主要用来解决一些和共享内存有关的问题,它允许指针被定义为从某一点开始算的32位偏移值,而不是内存种的绝对位置
    举个例子:typedef struct tagDEMOSTRUCT {
     int a;
     char sz[10];
    } DEMOSTRUCT, * PDEMOSTRUCT;HANDLE hFileMapping = CreateFileMapping(...);
    LPVOID lpShare = (LPDWORD)MapViewOfFile(...);DEMOSTRUCT __based(lpShare)* lpDemo;上面的例子声明了一个指针lpDemo,内部储存的是从lpShare开始的偏移值,也就是lpHead是以lpShare为基准的偏移值.
    上面的例子种的DEMOSTRUCT只是随便定义的一个结构,用来代表任意的结构.虽然__based指针使用起来非常容易,但是,你必须在效率上付出一定的代价.每当你用__based指针处理数据,CPU都必须
    为它加上基地址,才能指向真正的位置.
      

  2.   

    nnd
    强制转换
    lpKeyPH->lpHZPH=('struct _HZPH __based(lpMapFileBase) *)((LPBYTE)lpMapFileBase+
    dwBaseOffset+dwMapFileOffset);