将新对话框添加到旧版 C++ 应用程序 (VS2017) 会导致许多错误。为啥?

Posted

技术标签:

【中文标题】将新对话框添加到旧版 C++ 应用程序 (VS2017) 会导致许多错误。为啥?【英文标题】:Adding new Dialog to legacy C++ app (VS2017) results in many errors. Why?将新对话框添加到旧版 C++ 应用程序 (VS2017) 会导致许多错误。为什么? 【发布时间】:2018-12-26 14:06:48 【问题描述】:

这是一个旧版 C++ 应用程序。它已从 VS 6.0 移植到 VS2015,现在移植到 VS2017。 2017 年,我通过菜单/资源向导添加了一个对话框。此对话框的行为会导致 ATL 出现各种问题。 (错误示例如下...)

   c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.14.26428\atlmfc\include\afxbutton.h(184): warning C4003: not enough actual parameters for macro 'SelectFont'
\afxbutton.h(184): error C2146: syntax error: missing ')' before identifier 'SelectObject'
\afxbutton.h(184): error C3646: 'SelectObject': unknown override specifier
\afxbutton.h(184): error C2146: syntax error: missing ')' before identifier 'pDC'
\afxbutton.h(184): error C2065: 'pDC': undeclared identifier
\afxbutton.h(184): error C2096: 'HFONT': A data member cannot be initialized with a parenthesized initializer
\afxbutton.h(184): error C2433: 'CMFCButton::HFONT': 'virtual' not permitted on data declarations
\afxbutton.h(184): error C2131: expression did not evaluate to a constant
\afxbutton.h(184): note: failure was caused by non-constant arguments or reference to a non-constant symbol
\afxbutton.h(184): note: see usage of 'pDC'
\afxbutton.h(184): error C2327: 'CMFCButton::HFONT': is not a type name, static, or enumerator
\afxbutton.h(184): error C2059: syntax error: ')'
\afxbutton.h(184): error C2091: function returns function
\afxbutton.h(203): error C2327: 'CMFCButton::HFONT': is not a type name, static, or enumerator
\afxfontcombobox.h(45): warning C4003: not enough actual parameters for macro 'SelectFont'
\afxfontcombobox.h(45): error C2146: syntax error: missing ')' before identifier 'SelectObject'
\afxfontcombobox.h(45): error C3646: 'SelectObject': unknown override specifier
\afxfontcombobox.h(45): error C2146: syntax error: missing ')' before identifier 'pDesc'
\afxfontcombobox.h(45): error C2065: 'pDesc': undeclared identifier
\afxfontcombobox.h(45): error C2096: 'HFONT': A data member cannot be initialized with a parenthesized initializer
\afxfontcombobox.h(45): error C2131: expression did not evaluate to a constant
\afxfontcombobox.h(45): note: failure was caused by non-constant arguments or reference to a non-constant symbol
\afxfontcombobox.h(45): note: see usage of 'pDesc'
\afxfontcombobox.h(45): error C2327: 'CMFCFontComboBox::HFONT': is not a type name, static, or enumerator
\afxfontcombobox.h(45): error C2059: syntax error: ')'
\afxfontcombobox.h(45): error C2091: function returns function
\afxfontcombobox.h(46): error C2146: syntax error: missing ')' before identifier 'SelectObject'
\afxfontcombobox.h(46): error C3646: 'SelectObject': unknown override specifier
\afxfontcombobox.h(46): error C2146: syntax error: missing ')' before identifier 'lpszName'
\afxfontcombobox.h(46): error C2065: 'lpszName': undeclared identifier
\afxfontcombobox.h(46): error C2096: 'HFONT': A data member cannot be initialized with a parenthesized initializer
\afxfontcombobox.h(46): error C2086: 'BOOL CMFCFontComboBox::HFONT': redefinition
\afxfontcombobox.h(45): note: see declaration of 'CMFCFontComboBox::HFONT'
\afxfontcombobox.h(46): error C2131: expression did not evaluate to a constant
\afxfontcombobox.h(46): note: failure was caused by non-constant arguments or reference to a non-constant symbol
\afxfontcombobox.h(46): note: see usage of 'lpszName'
\afxfontcombobox.h(46): error C2327: 'CMFCFontComboBox::HFONT': is not a type name, static, or enumerator
\afxfontcombobox.h(46): error C2059: syntax error: ')'

罪魁祸首是 IDE 自动添加的#include "afxcontrolbars.h"。不确定我是否必须链接到另一个版本的 ATL、更改包含顺序或什么。有人有想法吗?

【问题讨论】:

SelectFont() 不是宏。除非您弄错了 #include 顺序并首先包含了 windowsx.h。我们看不到它。 向我们展示您在文件中包含的内容...对我来说,这看起来像是 cpp 文件顶部或任何第一个标题中的简单语法错误 【参考方案1】:

我在任何其他 afx 包含之前移动了 #include "afxcontrolbars.h",这解决了这个问题。

【讨论】:

感谢您发布此信息。我有同样的问题,这在 MFC 中修复了它。

以上是关于将新对话框添加到旧版 C++ 应用程序 (VS2017) 会导致许多错误。为啥?的主要内容,如果未能解决你的问题,请参考以下文章

Android构建警告将新ns映射到旧ns

将链表从 C 传递到 C++

在 Android Studio 中警告将新 ns 映射到旧 ns

警告:在严格模式树中检测到旧版上下文 API

将新 ns 映射到旧 ns

警告:将新 ns 映射到旧 ns 和模拟器突然停止