VC 6.0 转 VS 2010 问题:error C2440: 'static_cast' 解决办法

VC 6.0 转 VS 2010 问题:error C2440: 'static_cast'  

一定要注意,函数一定要带参数。

error C2440: “static_cast”: 无法从“void (__thiscall CChatDlg::* )(WPARAM,LPARAM)”转换为“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)”
1>        从基类型到派生类型的强制转换需要 dynamic_cast 或 static_cast

搜索发现为开发平台迁移问题。
由vc6.0升级至vs2005以上平台均会遇到这情况,原因为

VS2005 、VS 2010对消息的检查更为严格,以前在VC6下完全正常运行的消息映射在VS2005下编译不通过

// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnRecvData(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()

BEGIN_MESSAGE_MAP(CChatDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_EN_CHANGE(IDC_EDIT_RECV, &CChatDlg::OnEnChangeEditRecv)
ON_BN_CLICKED(IDC_BTN_SEND, &CChatDlg::OnBnClickedBtnSend)
ON_MESSAGE(WM_RECVDATA, &CChatDlg::OnRecvData)
END_MESSAGE_MAP()

编译错误提示:

error C2440: “static_cast”: 无法从“void (__thiscall CChatDlg::* )(WPARAM,LPARAM)”转换为“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)”
1>        从基类型到派生类型的强制转换需要 dynamic_cast 或
static_cast

更改定义:
afx_msg
void OnRecvData(WPARAM wParam, LPARAM lParam);

afx_msg LRESULT OnRecvData(WPARAM wParam, LPARAM lParam);

编译通过

LRESULT是一个数据类型,
MSDN: 32-bit value returned from a window procedure or callback function
指的是从窗口程序或者回调函数返回的32位值。

原文出处:http://blog.163.com/bbluesnow@126/blog/static/277845452011441511611/

### 如何在 Visual C++ 6.0 中读取 XML 文件 为了在 Visual C++ 6.0 中读取 XML 文件,可以利用 Microsoft 提供的 MSXML 库。MSXML 支持通过智能指针简化编程模型[^1]。 #### 创建项目并设置环境 首先,在 Visual C++ 6.0 中创建一个新的控制台应用程序项目。接着配置项目的属性以支持 COM 组件: - 添加对 `msxml6.dll` 的引用; - 设置预处理器定义 `_WIN32_WINNT=0x0501` 或更高版本来启用必要的 Windows 功能。 #### 初始化 COM 库 由于 MSXML 是基于 COM 技术构建的库,在使用之前需要初始化 COM 环境: ```cpp #include <windows.h> #include <comdef.h> // For _bstr_t and other COM types #include <msxml6.h> int main() { CoInitialize(NULL); // Initialize the COM library on the current thread try { // Your code here } catch (_com_error &e) { printf("Error: %s\n", (char*)e.Description()); } CoUninitialize(); // Uninitialize the COM library } ``` #### 加载和解析 XML 文档 下面展示了一个完整的例子,说明如何加载一个本地磁盘上的 XML 文件,并打印根节点名称及其第一个子元素的内容: ```cpp #include <iostream> using namespace std; // Include necessary headers for working with MSXML #import "C:\Program Files\Common Files\System\ado\msxml6.dll" \ named_guids rename("EOF","adEOF") void LoadAndParseXml(const char* filePath) { HRESULT hr; // Create an instance of the DOMDocument object using smart pointers _COM_SMARTPTR_TYPEDEF(IXMLDOMDocument, __uuidof(IXMLDOMDocument)); IXMLDOMDocumentPtr pDoc(__uuidof(DOMDocument)); // Set properties to ensure proper parsing behavior pDoc->async = VARIANT_FALSE; // Synchronous loading pDoc->validateOnParse = VARIANT_FALSE;// Do not validate against DTD or schema // Load the specified file into memory as a document tree structure VARIANT varPath; varPath.vt = VT_BSTR; varPath.bstrVal = _bstr_t(filePath); if (!pDoc->load(varPath)) throw new exception(); // Accessing elements within the parsed data structure cout << L"Root element name is '" << pDoc->documentElement->nodeName << "'." << endl; // Get first child node under root element IXMLDOMNodePtr pNode = pDoc->documentElement->firstChild; while(pNode != NULL && pNode->nodeType != NODE_ELEMENT){ pNode = pNode->nextSibling; } wcout << L"The content of its first sub-element is '" << static_cast<IXMLDOMText*>(pNode)->text << "'."; } int main(){ CoInitialize(NULL); try{ const wchar_t *path=L"C:\\example.xml"; // Replace this path accordingly LoadAndParseXml(path); }catch(_com_error& e){ cerr<<"An error occurred:"<<e.ErrorMessage()<<endl; } CoUninitialize(); return 0; } ``` 此程序展示了基本的操作流程,包括打开文件、访问文档结构以及提取特定的信息片段。对于更复杂的查询需求,则可能需要用到 XPath 表达式或其他高级特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值