请输入您要查询的百科知识:

 

词条 afxtempl
释义

axftemp1.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 \", 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.

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/2/27 2:10:26