複製鏈接
請複製以下鏈接發送給好友

afxtempl

鎖定
afxtempl.h是數據收集類模板(MFC template-based Collection Classes)的頭文件。
外文名
afxtempl
所屬分類
函數
倘若你在程序中用到了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.