Strange issue...
This is using VisualStudio Community 2017 and is a C++CLI WinForms project.
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();
};
The Problem:
In LuaReader.cpp If I include BGStatsInterface.h (needed so it can access the addMsg method), noted by PointA, it wont compile and will generate the errors listed below.
However, if I remove the GameCollection.h include from LuaReader.h (needed so I can create the GameCollection object), noted at PointB, it has no issues with BGStatsInterface.h included in the LuaReader.cpp file and everything compiles/runs without issues.
I don't know what to do... Why does it only work if I don't create the GameCollection object in LuaReader.h?
What have I done wrong?
Errors:
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
This happens when using #include Windows.h as it defines ::IServiceProvider while the framework has System::IServiceProvider, then when there's a using namespace System in scope somewhere it becomes ambiguous.
Solution is to simply put the delcarations in your own namespace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With