I have created a CLR empty project (.NET Framework) in Visual Studio, as I’ve learned that this is the preferred method to utilize C++ with an effective GUI. Within this project, I have a form named MyForm, with its associated file MyForm.h containing all the code.
I’ve placed some C++ classes in a file named MyClass.h, along with their implementations in a separate C++ file. Now, I wish to utilize one of these classes within my form. Initially, I included MyClass.h at the beginning of MyForm.h, and proceeded to create an object of that class. While it works for the most part – the form successfully updates its text using a function from the class – I now encounter an issue.
Attempting to open MyForm.h[designer] prompts the following error message:
C++ CodeDOM parser error: Line: 222, Column: 12 — Unknown type ‘MyClass’. Please ensure that the assembly containing this type is referenced. If this type is part of your development project, ensure that the project has been successfully built.***
I’ve struggled to find helpful resources online regarding the correct usage of a C++ class within a form file. As a student proficient in C++, I lack expertise in integrating it with other components. Additionally, within MyClass.h, I’m solely utilizing the std namespace. Could this potentially lead to namespace conflicts, or perhaps I need to reference my class or wrap it in a specific manner?"
I’ve attempted various troubleshooting steps, including cleaning and rebuilding the solutions, but unfortunately, I haven’t been able to find a solution
2
Answers
Moved the object instantiation and the call of its function to the myForm_Load function which has seemed to fix the issue
The problem is that the WinForms designer is .NET based, and doesn’t cope well with C++ native classes.
In these kind of applications I suggest splitting the code into 3 parts, each in a separate VS project:
GUI application. Can be written in C# which is more convenient IMHO, but if you prefer C++/CLI it can also work. This should be a bormal WinForms application project.
A C++/CLI wrapper around the native C++ logic. This should be a .NET DLL, with a thin layer that converts the native C++ classes into .NET ones. The GUI application will have a reference to this project and consume it. In order to create it in VS you can start with a C++ DLL project, and in the project settings add Common Language Runtime Support (/clr).
The native C++ classes. This can be either a native DLL or a static library. The .NET wrapper above should link with it.
Among other advatages this architecture will solve the problem you have with the GUI designer.