extern 全局变量申明出现错误
孤剑 2005-03-05 05:11:46 今天学习vc++作一个小东西,来练手,但是发现错误了,自己不知道怎么改了,请教各位了!
一般的变量申明是在。h文件中,所以我将全局变量的申明也放在了。h文件中,但是不知道位置是不是对的(因为我开始弄得时候,有一个朋友所我的声明位置放错了,导致了错误,后来没有仔细看,现在可能又出现了这样的问题)
1、**.h文件内容:
// FakeQQ.h : main header file for the FAKEQQ application
//
#if !defined(AFX_FAKEQQ_H__F2880776_08A7_48C4_B67F_4DC66555839D__INCLUDED_)
#define AFX_FAKEQQ_H__F2880776_08A7_48C4_B67F_4DC66555839D__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
/////////////////////////////////////////////////////////////////////////////
// CFakeQQApp:
// See FakeQQ.cpp for the implementation of this class
//
class CFakeQQApp : public CWinApp
{
public:
CFakeQQApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFakeQQApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CFakeQQApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//////////此处为我添加的内容
//FaceIcon[]为全局变量,以下为申明的全局变量
extern HICON FaceIcon[5]; //所有头像
extern CChatSocket ChatSocket; //聊天Socket
extern CUserInfo MyInfo; //当前用户信息
extern CDatabase ChatDB; //数据库
/////////添加内容结束
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_FAKEQQ_H__F2880776_08A7_48C4_B67F_4DC66555839D__INCLUDED_)
2、对应的**.cpp文件的内容:
// FakeQQ.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "FakeQQ.h"
#include "FakeQQDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFakeQQApp
BEGIN_MESSAGE_MAP(CFakeQQApp, CWinApp)
//{{AFX_MSG_MAP(CFakeQQApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFakeQQApp construction
///////////此处为我添加的内容
//配合FakeQQ.h中定义的变量
CFakeQQApp theApp;
HICON FaceIcon[];
CChatSocket ChatSocket;
CUserInfo MyInfo;
CDatabase ChatDB;
//后面的内容将陆续用到这些变量
//接下来,需要为CFakeQQDlg添加成员变量,
//用于保存用户头像的ImageList;
/////////添加内容结束
CFakeQQApp::CFakeQQApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CFakeQQApp object
/////////////////////////////////////////////////////////////////////////////
// CFakeQQApp initialization
BOOL CFakeQQApp::InitInstance()
{
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
//装载图片
UINT nFaceID[]={IDI_FACE0,IDI_FACE1,IDI_FACE2,IDI_FACE3,IDI_FACE4};
for(int i=0;i<sizeof(nFaceID)/sizeof(UINT);i++)
{
FaceIcon[i]=LoadIcon(nFaceID[i]);
//FaceIcon[]为全局变量,其申明在FakeQQ.h中
}
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CFakeQQDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
3、编译后的错误提示:
--------------------Configuration: FakeQQ - Win32 Debug--------------------
Compiling...
FakeQQ.cpp
f:\vvv\vc++\fakeqq\fakeqq.h(48) : error C2146: syntax error : missing ';' before identifier 'ChatSocket'
f:\vvv\vc++\fakeqq\fakeqq.h(48) : fatal error C1004: unexpected end of file found
Generating Code...
Compiling...
FakeQQDlg.cpp
f:\vvv\vc++\fakeqq\fakeqq.h(48) : error C2146: syntax error : missing ';' before identifier 'ChatSocket'
f:\vvv\vc++\fakeqq\fakeqq.h(48) : fatal error C1004: unexpected end of file found
Generating Code...
Error executing cl.exe.
FakeQQ.
请大家帮忙解答,谢谢!