创建一个c ++表单应用程序,我开始遇到一些模糊的符号错误。但我不太清楚是什么,它早些时候编译得很好,但我可能改变了一些东西?
Thee错误本身:
1>Application.cpp
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\ocidl.h(4089): error C2872: 'IDropTarget': ambiguous symbol
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\oleidl.h(3508): note: could be 'IDropTarget'
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\ocidl.h(4089): note: or 'System::Windows::Forms::IDropTarget'
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\ocidl.h(6496): error C2872: 'IServiceProvider': ambiguous symbol
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\servprov.h(98): note: could be 'IServiceProvider'
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\ocidl.h(6496): note: or 'System::IServiceProvider'
1>main.cpp
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\ocidl.h(4089): error C2872: 'IDropTarget': ambiguous symbol
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\oleidl.h(3508): note: could be 'IDropTarget'
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\ocidl.h(4089): note: or 'System::Windows::Forms::IDropTarget'
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\ocidl.h(6496): error C2872: 'IServiceProvider': ambiguous symbol
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\servprov.h(98): note: could be 'IServiceProvider'
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\ocidl.h(6496): note: or 'System::IServiceProvider'
我发现这很奇怪,因为我的表单不包含任何这些文件,我认为这是由于命名空间。我只是不知道这是从哪里来的。
以下是我的信息表格:
#include <Windows.h>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class CFormController : public Form
{
public:
CFormController(void)
{
InitializeComponent();
}
~CFormController()
{
delete components;
}
System::ComponentModel::Container ^components;
void InitializeComponent(void)
{
this->components = gcnew System::ComponentModel::Container();
this->Size = System::Drawing::Size(300, 300);
this->Text = L"FormController";
this->Padding = System::Windows::Forms::Padding(0);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
}
};
关于我的表单的唯一其他信息是在另一个类中。这只是用来显示或隐藏它。
我发现类似的问题是人们在“使用命名空间...”之前没有包含windows标题但是我添加了你看到的包含它并没有产生任何影响
答案 0 :(得分:1)
您正在将冲突的符号导入全局命名空间。
如果它是编译单元中包含的第一个文件,那么您发布的代码应该没问题。如果不是这种情况(例如,您在发布的头文件之前包含了另一个头文件),那么当您包含windows.h时,您可以在全局命名空间中存在WinForms类型,并导致C2872。
您可以将using语句或windows.h include移动到命名空间中以避免冲突。 例如
namespace Win32 {
#include "windows.h"
}
或
namespace ProjectName {
using namespace System;
......
}
或者,如果您确实要将类型导入全局命名空间,请将您的代码分成两个cpp文件,并一次导入一个。
答案 1 :(得分:-1)
C ++ / CLI因符号冲突而臭名昭着,无论何时使用&#34;使用&#34;许多C#命名空间。显然,System和System :: Windows :: Forms之间存在冲突。尝试删除后者&#34;使用&#34;声明,并通过在System :: Windows :: Forms ::前面添加前缀来指定此命名空间中的符号。我知道这是一种皇室般的痛苦,但据我所知,没有其他的解决办法。
每当我需要来自System :: Collections :: Generic的类时,我会经常遇到这个问题。