关于 "IServiceProvider" 的大量错误
Large amount of errors regarding "IServiceProvider"
奇怪的问题...
这是使用 VisualStudio Community 2017 的 C++CLI WinForms 项目。
BGStatsInterface.cpp
#include "BGStatsInterface.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
BattlegroundStats::BGStatsInterface form;
Application::Run(%form);
}
BGStatsInterface.h
#pragma once
#include <windows.h>
#include "LuaReader.h"
namespace BattlegroundStats {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
public ref class BGStatsInterface : public System::Windows::Forms::Form
{
public:
BGStatsInterface(void)
{
InitializeComponent();
this->MinimizeBox = false;
this->MaximizeBox = false;
}
static System::Windows::Forms::TextBox^ textBoxLog;
static System::Void addMsg(String^ text) {
textBoxLog->AppendText(text + "\n");
}
protected:
~BGStatsInterface()
{
if (components)
{
delete components;
}
}
private: LuaReader ^ reader = gcnew LuaReader();
private: System::Windows::Forms::Button^ b1;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->textBoxLog = (gcnew System::Windows::Forms::TextBox());
this->b1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// textBoxLog
//
this->textBoxLog->BackColor = System::Drawing::SystemColors::ControlLightLight;
this->textBoxLog->Cursor = System::Windows::Forms::Cursors::IBeam;
this->textBoxLog->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 6.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->textBoxLog->Location = System::Drawing::Point(10, 80);
this->textBoxLog->Multiline = true;
this->textBoxLog->Name = L"textBoxLog";
this->textBoxLog->ReadOnly = true;
this->textBoxLog->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
this->textBoxLog->Size = System::Drawing::Size(280, 90);
this->textBoxLog->TabIndex = 6;
this->textBoxLog->TabStop = false;
this->textBoxLog->GotFocus += gcnew System::EventHandler(this, &BGStatsInterface::textBoxLog_HideCaret);
//
// b1
//
this->b1->Location = System::Drawing::Point(100, 30);
this->b1->Name = L"b1";
this->b1->Size = System::Drawing::Size(75, 23);
this->b1->TabIndex = 7;
this->b1->Text = L"DoSomething";
this->b1->UseVisualStyleBackColor = true;
this->b1->Click += gcnew System::EventHandler(this, &BGStatsInterface::b1_Click);
//
// BGStatsInterface
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(301, 411);
this->Controls->Add(this->b1);
this->Controls->Add(this->textBoxLog);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
this->MaximumSize = System::Drawing::Size(317, 450);
this->MinimumSize = System::Drawing::Size(317, 450);
this->Name = L"BGStatsInterface";
this->Text = L"Test";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
System::Void textBoxLog_HideCaret(System::Object^ sender, System::EventArgs^ e) {
TextBox^ focus = safe_cast<TextBox^>(sender);
HideCaret((HWND)focus->Handle.ToPointer());
}
System::Void b1_Click(System::Object^ sender, System::EventArgs^ e) {
reader->DoSomething();
addMsg("DoingSomethingA");
System::Diagnostics::Debug::WriteLine("DoingSomethingA");
}
};
}
LuaReader.cpp
#include "LuaReader.h"
#include "BGStatsInterface.h" // PointA- This is where I'm having the issue.
using namespace System;
LuaReader::LuaReader(){}
System::Void LuaReader::DoSomething() {
BattlegroundStats::BGStatsInterface::addMsg("DoingSomethingB");
System::Diagnostics::Debug::WriteLine("DoingSomethingB");
}
LuaReader.h
#pragma once
#include "GameCollection.h" // PointB - Another issue here.
ref class LuaReader
{
public:
LuaReader();
GameCollection^ gameData = gcnew GameCollection();
System::String^ _fileName;
System::Void DoSomething();
};
#endif
GameCollection.cpp
#include "GameCollection.h"
GameCollection::GameCollection(){}
GameCollection.h
#pragma once
using namespace System;
ref class GameCollection
{
public:
GameCollection();
};
问题:
在 LuaReader.cpp 中,如果我包含 BGStatsInterface.h(需要它才能访问 addMsg 方法),PointA 指出,它不会编译并会生成下面列出的错误。
但是,如果我从 LuaReader.h 中删除 GameCollection.h 包含(需要这样我就可以创建 GameCollection 对象),在 PointB 中指出,它与包含在 BGStatsInterface.h 中的没有问题LuaReader.cpp 文件和所有 compiles/runs 都没有问题。
我不知道该怎么做...为什么只有在 LuaReader.h 中不创建 GameCollection 对象时它才有效?
我做错了什么?
错误:
Severity Code Description Project File Line Suppression State
Error (active) E1986 an ordinary pointer to a C++/CLI ref class or interface class is not allowed BattlegroundStats c:\Program Files (x86)\Windows Kits\Include.0.16299.0\um\servprov.h 91
Error (active) E0266 "IServiceProvider" is ambiguous BattlegroundStats c:\Program Files (x86)\Windows Kits\Include.0.16299.0\um\servprov.h 115
Error (active) E0266 "IServiceProvider" is ambiguous BattlegroundStats c:\Program Files (x86)\Windows Kits\Include.0.16299.0\um\servprov.h 239
Error (active) E0266 "IServiceProvider" is ambiguous BattlegroundStats c:\Program Files (x86)\Windows Kits\Include.0.16299.0\um\servprov.h 249
Error (active) E0266 "IServiceProvider" is ambiguous BattlegroundStats c:\Program Files (x86)\Windows Kits\Include.0.16299.0\um\urlmon.h 6867
Error (active) E0266 "IServiceProvider" is ambiguous BattlegroundStats c:\Program Files (x86)\Windows Kits\Include.0.16299.0\um\urlmon.h 6869
Error C3699 '*': cannot use this indirection on type 'IServiceProvider' BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\servprov.h 91
Error C2371 'IServiceProvider': redefinition; different basic types BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\servprov.h 98
Error C2872 'IServiceProvider': ambiguous symbol BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\servprov.h 115
Error C2872 'IServiceProvider': ambiguous symbol BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\servprov.h 239
Error C2872 'IServiceProvider': ambiguous symbol BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\servprov.h 249
Error C2872 'IServiceProvider': ambiguous symbol BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\urlmon.h 6867
Error C2872 'IServiceProvider': ambiguous symbol BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\urlmon.h 6869
当使用 #include Windows.h 时会发生这种情况,因为它定义了 ::IServiceProvider 而框架有 System::IServiceProvider,然后当范围内某处有 using 命名空间系统时,它会变得不明确。
解决方案是简单地将声明放在您自己的命名空间中。
如果您的项目较小,请在此提供另一种解决方案 - 删除:
using namespace System;
并在系统 class 的代码中使用完整的类型名称,例如System::String^
.
要解决此问题,请在声明名称空间 and/or 之前放入包含文件(其中包含 windows.h):
所以:
#include "stdafx.h"
using namespace System;
而不是
using namespace System;
#include "stdafx.h"
致敬
奇怪的问题...
这是使用 VisualStudio Community 2017 的 C++CLI WinForms 项目。
BGStatsInterface.cpp
#include "BGStatsInterface.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
BattlegroundStats::BGStatsInterface form;
Application::Run(%form);
}
BGStatsInterface.h
#pragma once
#include <windows.h>
#include "LuaReader.h"
namespace BattlegroundStats {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
public ref class BGStatsInterface : public System::Windows::Forms::Form
{
public:
BGStatsInterface(void)
{
InitializeComponent();
this->MinimizeBox = false;
this->MaximizeBox = false;
}
static System::Windows::Forms::TextBox^ textBoxLog;
static System::Void addMsg(String^ text) {
textBoxLog->AppendText(text + "\n");
}
protected:
~BGStatsInterface()
{
if (components)
{
delete components;
}
}
private: LuaReader ^ reader = gcnew LuaReader();
private: System::Windows::Forms::Button^ b1;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->textBoxLog = (gcnew System::Windows::Forms::TextBox());
this->b1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// textBoxLog
//
this->textBoxLog->BackColor = System::Drawing::SystemColors::ControlLightLight;
this->textBoxLog->Cursor = System::Windows::Forms::Cursors::IBeam;
this->textBoxLog->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 6.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->textBoxLog->Location = System::Drawing::Point(10, 80);
this->textBoxLog->Multiline = true;
this->textBoxLog->Name = L"textBoxLog";
this->textBoxLog->ReadOnly = true;
this->textBoxLog->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
this->textBoxLog->Size = System::Drawing::Size(280, 90);
this->textBoxLog->TabIndex = 6;
this->textBoxLog->TabStop = false;
this->textBoxLog->GotFocus += gcnew System::EventHandler(this, &BGStatsInterface::textBoxLog_HideCaret);
//
// b1
//
this->b1->Location = System::Drawing::Point(100, 30);
this->b1->Name = L"b1";
this->b1->Size = System::Drawing::Size(75, 23);
this->b1->TabIndex = 7;
this->b1->Text = L"DoSomething";
this->b1->UseVisualStyleBackColor = true;
this->b1->Click += gcnew System::EventHandler(this, &BGStatsInterface::b1_Click);
//
// BGStatsInterface
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(301, 411);
this->Controls->Add(this->b1);
this->Controls->Add(this->textBoxLog);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
this->MaximumSize = System::Drawing::Size(317, 450);
this->MinimumSize = System::Drawing::Size(317, 450);
this->Name = L"BGStatsInterface";
this->Text = L"Test";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
System::Void textBoxLog_HideCaret(System::Object^ sender, System::EventArgs^ e) {
TextBox^ focus = safe_cast<TextBox^>(sender);
HideCaret((HWND)focus->Handle.ToPointer());
}
System::Void b1_Click(System::Object^ sender, System::EventArgs^ e) {
reader->DoSomething();
addMsg("DoingSomethingA");
System::Diagnostics::Debug::WriteLine("DoingSomethingA");
}
};
}
LuaReader.cpp
#include "LuaReader.h"
#include "BGStatsInterface.h" // PointA- This is where I'm having the issue.
using namespace System;
LuaReader::LuaReader(){}
System::Void LuaReader::DoSomething() {
BattlegroundStats::BGStatsInterface::addMsg("DoingSomethingB");
System::Diagnostics::Debug::WriteLine("DoingSomethingB");
}
LuaReader.h
#pragma once
#include "GameCollection.h" // PointB - Another issue here.
ref class LuaReader
{
public:
LuaReader();
GameCollection^ gameData = gcnew GameCollection();
System::String^ _fileName;
System::Void DoSomething();
};
#endif
GameCollection.cpp
#include "GameCollection.h"
GameCollection::GameCollection(){}
GameCollection.h
#pragma once
using namespace System;
ref class GameCollection
{
public:
GameCollection();
};
问题:
在 LuaReader.cpp 中,如果我包含 BGStatsInterface.h(需要它才能访问 addMsg 方法),PointA 指出,它不会编译并会生成下面列出的错误。
但是,如果我从 LuaReader.h 中删除 GameCollection.h 包含(需要这样我就可以创建 GameCollection 对象),在 PointB 中指出,它与包含在 BGStatsInterface.h 中的没有问题LuaReader.cpp 文件和所有 compiles/runs 都没有问题。
我不知道该怎么做...为什么只有在 LuaReader.h 中不创建 GameCollection 对象时它才有效?
我做错了什么?
错误:
Severity Code Description Project File Line Suppression State
Error (active) E1986 an ordinary pointer to a C++/CLI ref class or interface class is not allowed BattlegroundStats c:\Program Files (x86)\Windows Kits\Include.0.16299.0\um\servprov.h 91
Error (active) E0266 "IServiceProvider" is ambiguous BattlegroundStats c:\Program Files (x86)\Windows Kits\Include.0.16299.0\um\servprov.h 115
Error (active) E0266 "IServiceProvider" is ambiguous BattlegroundStats c:\Program Files (x86)\Windows Kits\Include.0.16299.0\um\servprov.h 239
Error (active) E0266 "IServiceProvider" is ambiguous BattlegroundStats c:\Program Files (x86)\Windows Kits\Include.0.16299.0\um\servprov.h 249
Error (active) E0266 "IServiceProvider" is ambiguous BattlegroundStats c:\Program Files (x86)\Windows Kits\Include.0.16299.0\um\urlmon.h 6867
Error (active) E0266 "IServiceProvider" is ambiguous BattlegroundStats c:\Program Files (x86)\Windows Kits\Include.0.16299.0\um\urlmon.h 6869
Error C3699 '*': cannot use this indirection on type 'IServiceProvider' BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\servprov.h 91
Error C2371 'IServiceProvider': redefinition; different basic types BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\servprov.h 98
Error C2872 'IServiceProvider': ambiguous symbol BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\servprov.h 115
Error C2872 'IServiceProvider': ambiguous symbol BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\servprov.h 239
Error C2872 'IServiceProvider': ambiguous symbol BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\servprov.h 249
Error C2872 'IServiceProvider': ambiguous symbol BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\urlmon.h 6867
Error C2872 'IServiceProvider': ambiguous symbol BattlegroundStats c:\program files (x86)\windows kits\include.0.16299.0\um\urlmon.h 6869
当使用 #include Windows.h 时会发生这种情况,因为它定义了 ::IServiceProvider 而框架有 System::IServiceProvider,然后当范围内某处有 using 命名空间系统时,它会变得不明确。
解决方案是简单地将声明放在您自己的命名空间中。
如果您的项目较小,请在此提供另一种解决方案 - 删除:
using namespace System;
并在系统 class 的代码中使用完整的类型名称,例如System::String^
.
要解决此问题,请在声明名称空间 and/or 之前放入包含文件(其中包含 windows.h):
所以:
#include "stdafx.h"
using namespace System;
而不是
using namespace System;
#include "stdafx.h"
致敬