afxtempl

afxtempl.h是數據收集類模板(MFC template-based Collection Classes)的頭檔案。

倘若你在程式中用到了lists,maps或者arrays等數據結構時,那么最好載入該檔案,因為通常在MFC編程中,MSDN建議使用基於模板的(template-based)數據收集類。要在StdAfx.h中加入下面語句:
#include <afxtempl.h>
那么如何在win32 console程式中使用MFC模板類呢?編譯器環境該如何設定呢?這裡以VC++ 6.0 為例介紹其使用方法。先看下面一段程式:
#include "afxtempl.h"
class CAction : public CObject
{
public:
CAction(int nTime)
{
m_nTime = nTime;
}
void PrintTime() { TRACE("time = %d \n", m_nTime);}
private:
int m_nTime;
};
int main()
{
CAction* pAction;
CObList actionList;
for (int i = 0; i < 5; i ++)
{
pAction = new CAction(i);
actionList.AddTail(pAction);
}
while (!actionList.IsEmpty())
{
pAction = (CAction*) actionList.RemoveHead();
pAction->PrintTime();
delete pAction;
}
return 0;
}
這裡有必要介紹一下CObList類的特性。CObList是MFC集合類的一種(另一種是CPtrList),能夠支持指向CObject派生類對象的指針列表。上面程式中的CAction類就是派生自CObject。CObList的一個很重要的特性就是可以包含混合的指針,換句話說,一個CObList既可以包含CStudent對象的指針,也可以包含CTeacher對象的指針,前提是他們都是派生自CObject。
上面的程式我們用默認的編譯器設定就會出現編譯或連結錯:
Compiling...
Command line warning D4002 : ignoring unknown option '/'
file.cpp
Linking...
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/win32console6.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
win32console6.exe - 3 error(s), 1 warning(s)
針對這種情況,侯捷的《深入淺出MFC》(第二版)P37給出了解釋:在MFC console程式中一定要指定多執行緒的C runtime 函式館,所以必須使用/MT選項。即在6.0編譯器下的具體設定:
project settings dialogà C/C++tab à category: select code generationà use run-time library : select multithreaded or debug multithreaded.

相關詞條

熱門詞條

聯絡我們