有关“ IServiceProvider”的大量错误

时间:2018-07-05 15:43:58

标签: winforms visual-studio-2017 c++-cli

奇怪的问题...

这使用的是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中,如果我包含PointA指出的BGStatsInterface.h(需要它可以访问addMsg方法),它将无法编译,并会产生下面列出的错误。

但是,如果我从LuaReader.h中删除GameCollection.h包含项(需要,这样我就可以创建GameCollection对象),如PointB所述,它与LuaReader.cpp文件中包含的BGStatsInterface.h都没有问题,并且所有内容都可以编译/运行没有问题。

我不知道该怎么办...为什么我只有在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\10\Include\10.0.16299.0\um\servprov.h   91  
Error (active)  E0266   "IServiceProvider" is ambiguous BattlegroundStats   c:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\servprov.h   115 
Error (active)  E0266   "IServiceProvider" is ambiguous BattlegroundStats   c:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\servprov.h   239 
Error (active)  E0266   "IServiceProvider" is ambiguous BattlegroundStats   c:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\servprov.h   249 
Error (active)  E0266   "IServiceProvider" is ambiguous BattlegroundStats   c:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\urlmon.h 6867    
Error (active)  E0266   "IServiceProvider" is ambiguous BattlegroundStats   c:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\urlmon.h 6869        
Error   C3699   '*': cannot use this indirection on type 'IServiceProvider' BattlegroundStats   c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\servprov.h   91  
Error   C2371   'IServiceProvider': redefinition; different basic types BattlegroundStats   c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\servprov.h   98  
Error   C2872   'IServiceProvider': ambiguous symbol    BattlegroundStats   c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\servprov.h   115 
Error   C2872   'IServiceProvider': ambiguous symbol    BattlegroundStats   c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\servprov.h   239 
Error   C2872   'IServiceProvider': ambiguous symbol    BattlegroundStats   c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\servprov.h   249 
Error   C2872   'IServiceProvider': ambiguous symbol    BattlegroundStats   c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\urlmon.h 6867    
Error   C2872   'IServiceProvider': ambiguous symbol    BattlegroundStats   c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\urlmon.h 6869    

3 个答案:

答案 0 :(得分:1)

在框架包含System :: IServiceProvider的情况下使用#include Windows.h定义了:: IServiceProvider时,会发生这种情况,然后当范围中存在使用命名空间的System变得模糊时。

解决方案是将代币简单地放在您自己的名称空间中。

答案 1 :(得分:0)

如果您的项目很小,请在此处提供另一个解决方案。删除

/mnt/efs/media

,并在系统类的代码中使用全名。例如System :: String ^

答案 2 :(得分:0)

要解决此问题,请在声明名称空间和/或使用语句之前,将包含文件(其中包含windows.h)放入其中:

所以:

#include "stdafx.h"

using namespace System;

代替

using namespace System;

#include "stdafx.h"

http://www.windows-tech.info/17/fbe07cd4dfcac878.php的帽子提示