设为首页 加入收藏

TOP

VC6代码移植到高版本VC时候的常见问题
2014-11-23 19:19:24 】 浏览:5902
Tags:VC6 代码 移植 版本 时候 常见问题

最近把一个VC6工程移植到高版本VC时候,总结的一些代码需要修改的地方:

1、循环体变量的作用域的差异

VC6可以这样写,循环变量在循环体外面一样有效:
for(int i=0; i {
...
}
...
for(i=0; i {
...
}

高版本VC则限定了循环体变量的作用域,需要改成下面这样:
for(int i=0; i {
...
}
...
for(int i=0; i {
...
}

2、变量定义时候的默认类型

VC6中如果一个变量未定义类型,则按照默认类型(int类型)来处理,不会报错,例如下面这行代码在VC6中默认是可以编译的
static g_bSplashTimer = FALSE;

高版本VC则必须要定义变量类型,所以上面的代码编译时候会报错,需要修改为:
static BOOL g_bSplashTimer = FALSE;

函数返回值定义同样存在这样的问题。

3、类型转换问题

class CToolStatusPane : public CObject
{
public:
CToolStatusPane();
~CToolStatusPane();
CToolStatusPane(const CToolStatusPane& other)
{
m_nID = other.m_nID;
m_nWidth = other.m_nWidth;
m_strText = other.m_strText;
m_strTooltip = other.m_strTooltip;
m_strIcon = other.m_strIcon;
m_strAction = other.m_strAction;
}
CToolStatusPane& operator = (const CToolStatusPane& other)
{
m_nID = other.m_nID;
m_nWidth = other.m_nWidth;
m_strText = other.m_strText;
m_strTooltip = other.m_strTooltip;
m_strIcon = other.m_strIcon;
m_strAction = other.m_strAction;
return *this;
}
public:
UINT m_nID; // Pane ID
int m_nWidth; // Pane宽度
CString m_strText; // Pane文字
CString m_strTooltip; // Pane提示信息
CString m_strIcon; // Pane图标
CString m_strAction; // 动作
};
typedef CArray CToolStatusPaneArray;

for(int i=0; i {
m_arStatusPane.Add(other.m_arStatusPane[i]);
}

上面这段代码在VC6可以编译,高版本VC编译时候会报下面的错误:
error C2664: “CArray::Add”: 不能将参数1 从“const CToolStatusPane”转换为“CToolStatusPane &”
with
[
TYPE=CToolStatusPane,
ARG_TYPE=CToolStatusPane &
]
转换丢失限定符

改成如下代码可以编译通过:
for(int i=0; i {
m_arStatusPane.Add((CToolStatusPane &)other.m_arStatusPane[i]);
}

应该是因为高版本VC对于类型检查更严格造成的。

4、CString的变化

一个从CString派生的类,在VC6可以编译,高版本VC编译出错,代码如下:

class CReportData : public CString
{
public:
CReportData();
~CReportData();

BOOL New(INT iSubItems);

BOOL GetSubItem(INT iSubItem, LPINT lpiImage, LPINT lpiOverlay, LPINT lpiCheck, LPINT lpiColor, LPTSTR lpszText, LPINT lpiTextMax);
BOOL SetSubItem(INT iSubItem, INT iImage, INT iOverlay, INT iCheck, INT iColor, LPCTSTR lpszText);

BOOL InsertSubItem(INT iSubItem, INT iImage, INT iOverlay, INT iCheck, INT iColor, LPCTSTR lpszText);
BOOL DeleteSubItem(INT iSubItem);
};

BOOL CReportData::GetSubItem(INT iSubItem, LPINT lpiImage, LPINT lpiOverlay, LPINT lpiCheck, LPINT lpiColor, LPTSTR lpszText, LPINT lpiTextMax)
{
...
LPCTSTR lpsz = m_pchData;
...
}

在高版本VC中编译时候会报变量m_pchData找不到,原因是m_pchData在VC6的MFC版本中CString类是有这个成员变量的,高版本的已经没有了,替代方法是改成下面这样:
LPCTSTR lpsz = (LPTSTR)GetString();

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇vc++笔记---CDatabase类 下一篇VC++任务栏托盘图标及右键菜单实..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目