gpt4 book ai didi

c++ - Visual Studio 警告 C4100 和 TRACE

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:46 24 4
gpt4 key购买 nike

在 Visual Studio 2012 中,当我尝试编译以下 C++ 函数时:

void CCustToolBar::OnCustHelp(NMHDR* pNMHDR, LRESULT* /*pResult*/)
{
TRACE(_T("{ Help ID = %d }\n"), pNMHDR->idFrom);
}

我收到警告“warning C4100: 'pNMHDR' : unreferenced formal parameter”

这没有意义,因为正在使用 pNMHDR。如果我尝试将其注释掉:

void CCustToolBar::OnCustHelp(NMHDR* /*pNMHDR*/, LRESULT* /*pResult*/)
{
TRACE(_T("{ Help ID = %d }\n"), pNMHDR->idFrom);
}

我收到错误“错误 C2065:‘pNMHDR’:未声明的标识符”

这确实有道理。

我在这里错过了什么?为什么我在使用变量时收到警告 C4100?

最佳答案

TRACE 的 MSDN 页面说:

In the debug version of MFC, this macro sends the specified string to the debugger of the current application. In a release build, this macro compiles to nothing (no code is generated at all).

因此,您必须在 Release模式下编译才能收到警告,因为整个 TRACE 调用将不存在于该配置中。

如果您将代码更改为:

void CCustToolBar::OnCustHelp(NMHDR* pNMHDR, LRESULT* /*pResult*/)
{
#ifdef DEBUG
TRACE(_T("{ Help ID = %d }\n"), pNMHDR->idFrom);
#else
UNREFERENCED_PARAMETER(pNMHDR);
#endif
}

这将是一种干净的方法,可以避免 Release模式下的警告,同时保留调试的预期功能。

关于c++ - Visual Studio 警告 C4100 和 TRACE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23112322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com