现在的位置: 首页 > 综合 > 正文

为什么头文件顺序不能乱写?为什么在win32控制台应用程序中不能使用CString?

2014年09月05日 ⁄ 综合 ⁄ 共 2405字 ⁄ 字号 评论关闭

为什么头文件顺序不能乱写?为什么在win32控制台应用程序中不能使用CString?

----------------------------首先第1问:为什么头文件顺序不能乱写?----------------------------------

在讲此问题时,专门用了一个MFC的类对象CString(注:本例是win32控制台应用程序),注:要使用CString要经过2步:第1步:win32控制台默认情况下不用MFC类,所以要改成用MFC类(在工程-->设置-->常规属性中改),动态静态无所谓 第2步:添加头文件#include <afx.h>

------------一共有2个错误提示:第1个,即第2步头文件的错误 第2个,即第1步的MFC类的错误-------------

----------错误顺序:主要介绍第2步:头文件---------

//#include <afx.h>         //正确顺序:放在最前面
#include<iostream>
using namespace std;
#include <afx.h>         //错误顺序:放在最后面,会造成错误
//测试后的错误结果://#include <string.h>//#include <windows.h>//#include <afxstr.h>

int main()
{
CString str;//string str;//此处有个string类型,是属于控制台专门的字符串变量类型,不需要头文件,直接能用
return 0;
}

------------------错误提示:

--------------------Configuration: 12 - Win32 Debug--------------------
Compiling...
were.cpp
c:\program files\microsoft visual studio\vc98\mfc\include\afx.h(438) : fatal error C1001: INTERNAL COMPILER ERROR
        (compiler file 'msc1.cpp', line 1786)
         Please choose the Technical Support command on the Visual C++
         Help menu, or open the Technical Support help file for more information
Error executing cl.exe.

were.obj - 1 error(s), 0 warning(s)
-------------------------正确顺序:上面注释里已提过了

--------------------------错误顺序:第2个,主讲第1步中的MFC类的错误提示---------

在第1个的基础上,如果不用MFC类,会出现什么错误?

错误提示如下:

--------------------Configuration: 12 - Win32 Debug--------------------
Linking...
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/12.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

12.exe - 3 error(s), 0 warning(s)
注:以后当你在看到此行错误时,你就要联想到,是不是MFC类出了错,

--------------------第2问:为什么在win32控制台应用程序中不能使用CString?-------------------------

在经过一番对CString的检查后,现用一个实例来测试下

怎样从以下字符串中str="$PASHR,PBN,473330.00,6378137.0,0000000.0,0000000.0,000:00.000000,000:00.000000,00000.000,000.00,000.00,000.00,00,????,00,00,00,00*17";
获取473330.00值?

---------源代码如下:在第1问正确的基础上,添加

#include <afx.h>         //正确顺序:放在最前面
#include<iostream>
using namespace std;
//#include <afx.h>         //错误顺序:放在最后面,会造成错误
//测试后的错误结果://#include <string.h>//#include <windows.h>//#include <afxstr.h>

int main()
{
CString str;
str="$PASHR,PBN,473330.00,6378137.0,0000000.0,0000000.0,000:00.000000,000:00.000000,00000.000,000.00,000.00,000.00,00,????,00,00,00,00*17";
  
int startPos = str.Find("PBN,");
if( startPos >= 0 )
{
   startPos += 4;
   int lastPos = str.Find( ",", startPos );
   CString strRes = str.Mid( startPos, lastPos-startPos);MessageBox(0,strRes,"ok",MB_OK);
}
return 0;
}

抱歉!评论已关闭.