读书人

各位牛人小弟有一事不明给分100,该

发布时间: 2012-02-29 16:44:10 作者: rapoo

各位牛人,小弟有一事不明,给分100
我在appprot.h 中定义如下
#ifndef APPPROT_H
#define APPRPOT_H
struct app_solo_head
{
char app_state_flag;
char app_data_type;
int len;
char app_serve_type;
char node_type;
};

#define APPHEADBUF_SIZE sizeof(app_solo_head)
extern app_solo_head *apphead;
extern char appheadBUF[APPHEADBUF_SIZE];
#endif

我在appprot.cpp 中定义如下
#include "stdafx.h "
#include "appprot.h "
struct app_solo_head *apphead;
char appheadBUF[APPHEADBUF_SIZE];

apphead = (struct app_solo_head *)( &appheadBUF[0] );

然后我编译了一下,出现如下三个错误,请大虾帮忙;
Compiling...
appprot.cpp
C:\Documents and Settings\peifei\桌面\client\appprot.cpp(9) : error C2501: 'apphead ' : missing storage-class or type specifiers
C:\Documents and Settings\peifei\桌面\client\appprot.cpp(9) : error C2040: 'apphead ' : 'int ' differs in levels of indirection from 'struct app_solo_head * '
C:\Documents and Settings\peifei\桌面\client\appprot.cpp(9) : error C2440: 'initializing ' : cannot convert from 'struct app_solo_head * ' to 'int '
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.

appprot.obj - 3 error(s), 0 warning(s)



[解决办法]
apphead = (struct app_solo_head *)( &appheadBUF[0] );不能放到全局空间中,应该放到函数中
[解决办法]
错误的根源就在于
apphead = (struct app_solo_head *)( &appheadBUF[0] );
这条语句. 不过倒不是它本身有什么错,而是你把它放错了地方: 它不能出现在全局定义中.
须知全局定义只能是declare(变量声明之类),而不能是这种statement语句

修改方法有两种:
1.把它放到main()或其它任何函数体中.
或者:
2. 将这三行:
struct app_solo_head *apphead;
char appheadBUF[APPHEADBUF_SIZE];

apphead = (struct app_solo_head *)( &appheadBUF[0] );

修改成:
char appheadBUF[APPHEADBUF_SIZE];
struct app_solo_head *apphead = (struct app_solo_head *)( &appheadBUF[0] );


[解决办法]
jf

读书人网 >C++

热点推荐