"error C2653: System is not a class or a namespace name"在窗体头文件、可视C++

"error C2653: System is not a class or a namespace name" in Form header file, Visual C++

本文关键字:窗体 文件 C++ 可视 name or C2653 error System is class      更新时间:2023-10-16

我之前关于同一个项目的问题:第一和第二。没有必要去读它们;只知道我正试图在Visual c++项目中使用本地c++ SDK。这比我最初想象的要棘手得多,但是这个关于用托管代码扩展本地c++项目的网站已经帮助了我很多。

根据最后一个链接的说明,我在我的本地c++项目中添加了一个Form,它自动将项目转换为CLR。只有MainForm.cppInterface.cpp(允许本地c++代码创建和显示MainForm的文件)是用/clr标志编译的;其他文件保持原生。

我现在的问题是,Visual Studio似乎不识别MainForm.h中使用的任何CLR东西。因此,在以下行中:

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

System总是用红色下划线,有相应的错误:

error C2653: 'System' is not a class or a namespace name

它也不识别gcnew这个词和其他应该在CLR中轻松工作的东西。

谁能告诉我我可能做错了什么?我猜是非常小的东西;一些我忘记更改的标志,缺少引用或类似的东西,但我就是不知道它是什么

在哪里包含MainForm.h -直接还是间接?
如果在Interface.h中包含MainForm.h,则在包含Interface.h的任何地方也间接包含它。这意味着,如果你在任何没有使用/clr编译的翻译单元(即*.cpp)中包含Interface.h,那么编译器当然会抱怨它,因为namespace Systemgcnew不是标准c++的一部分。

因此你应该只在Interface.cpp中包含MainForm.h,在Interface.h中使用前向声明。