文章类型: VC&C++
关键词: VC6代码移植到高版本VC时候的常见问题
内容摘要: VC6代码移植到高版本VC时候的常见问题

VC6代码移植到高版本VC时候的常见问题

2016/8/16 16:31:13    来源:apple    阅读:

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

 

1、循环体变量的作用域的差异
 
VC6可以这样写,循环变量在循环体外面一样有效:
for(int i=0; i<m_aPlugIns.GetSize(); i++)
{
    ...
}
...
for(i=0; i<m_aPlugIns.GetSize(); i++)
{
    ...
}
 
高版本VC则限定了循环体变量的作用域,需要改成下面这样:
for(int i=0; i<m_aPlugIns.GetSize(); i++)
{
    ...
}
...
for(int i=0; i<m_aPlugIns.GetSize(); 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<CToolStatusPane, CToolStatusPane&> CToolStatusPaneArray;
 
for(int i=0; i<other.m_arStatusPane.GetSize(); i++)
{
     m_arStatusPane.Add(other.m_arStatusPane[i]);
}
 
上面这段代码在VC6可以编译,高版本VC编译时候会报下面的错误:
error C2664: “CArray<TYPE,ARG_TYPE>::Add”: 不能将参数1 从“const CToolStatusPane”转换为“CToolStatusPane &”
        with
        [
            TYPE=CToolStatusPane,
            ARG_TYPE=CToolStatusPane &
        ]
        转换丢失限定符
 
改成如下代码可以编译通过:
for(int i=0; i<other.m_arStatusPane.GetSize(); 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();
 

当然,还有一些其他的转换问题,有时间了再补充写了。

↑ 上一篇文章:VS2010之MFC入门到精通教程(1-55全部) 关键词:VC,C++,MFC,教程 发布日期:2016/8/16 15:45:21
↓ 下一篇文章:VC实现显示、自动隐藏任务栏(SHAppBarMessage) 关键词:VC,实现显示,自动隐藏任务栏,SHAppBarMess.. 发布日期:2016/8/17 11:03:06
相关目录:.NETVC&C++软件开发
我要评论
正在加载评论信息......