首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

error C2065: 'AfxWinInit' : undeclared identifier,该怎么解决

2012-02-19 
error C2065: AfxWinInit : undeclared identifier[codeC/C++][/code]#include afx.h#include iostr

error C2065: 'AfxWinInit' : undeclared identifier
[code=C/C++][/code]
#include <afx.h> 
#include <iostream> 
using namespace std;

void Recurse(LPCTSTR pstr) 

  CFileFind finder; 

  // build a string with wildcards 
  CString strWildcard(pstr); 
  strWildcard += _T("//*.*"); 

  // start working for files 
  BOOL bWorking = finder.FindFile(strWildcard); 

  while (bWorking) 
  { 
  bWorking = finder.FindNextFile(); 

  // skip . and .. files; otherwise, we'd 
  // recur infinitely! 

  if (finder.IsDots()) 
  continue; 

  // if it's a directory, recursively search it 

  if (finder.IsDirectory()) 
  { 
  CString str = finder.GetFilePath(); 
  cout << (LPCTSTR) str << endl; 
  Recurse(str); 
  } 
  } 

  finder.Close(); 


void _tmain() 

if (!AfxWinInit(::GetModuleHandle(NULL), NULL,::GetCommandLine(), 0))
  cout << "panic!" << endl; 
  else 
  Recurse(_T("C:")); 


请问一下,为什么会出现error C2065: 'AfxWinInit' : undeclared identifier这种错误?

[解决办法]
摘自MSDN
// this file must be compiled with the /GX and /MT options:
// cl /GX /MT thisfile.cpp

#include <afx.h>
#include <afxdb.h>
#include <iostream.h>

int main()
{
// try to initialize MFC

if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
cerr << "MFC failed to initialize!" << endl;
return 1;
}

// try to connect to an ODBC database that doesn't exist
// (this wouldn't work at all without initializing MFC)

CDatabase db;
try
{
db.Open("This Databsae Doesn't Exist");

// we shouldn't realistically get here

cout << "Successful!" << endl;
cout << "Closing ... ";
db.Close();
cout << "Closed!" << endl;
}
catch (CDBException* pEx)
{
// we got an exception! print an error message
// (this wouldn't work without initializing MFC)

char sz[1024];

cout << "Error: ";
if (pEx->GetErrorMessage(sz, 1024))
cout << sz;
else
cout << "No error message was available";
cout << endl;

pEx->Delete();
return 1;
}

return 0;
}


[解决办法]

热点排行