如何使用 OutputDebugString 添加指针

How to use OutputDebugString to add pointer

本文关键字:指针 添加 OutputDebugString 何使用      更新时间:2023-10-16

我有调试模式下的代码:

OutputDebugString(_T("Element Name = ") + (Node->getParentElement() == NULL ? "null" : Node->getParentElement()->getName()) + _T("n"));
 //getname() type is CString and GetParentElement() type is CXMLElement

使用此代码,我得到以下错误:错误 C2110:"+":无法添加两个指针。我知道不能添加两个指针。

我应该使用什么 API 来清除此错误?

您可以按如下方式使用它:

TCHAR msgbuf[256]; //keep required size
sprintf(msgbuf, "The value is %sn", charPtrVariable);
OutputDebugString(msgbuf);

由于问题被标记为 C++ ,我建议使用 stringstream:

#include <sstream>
//...
std::stringstream ss;
ss << "Element Name = " 
   << (Node->getParentElement() == NULL ? "null" : Node->getParentElement()->getName()) 
   << std::endl;
OutputDebugString(ss.str().c_str());

由于不能将两个指针相加以连接字符串,因此可以使用临时CString对象并追加到该对象:

CString tmp = _T("Element Name = ");
tmp += Node->getParentElement() == NULL ? "null" : Node->getParentElement()->getName();
tmp += _T("n");
OutputDebugString(tmp);