Classes declared with ref keyword are "managed" classes, i.e. they're managed by the Microsoft.NET runtime.
AFAIK, a "non-managed" class, such as your Building class, is not allowed to contain a handle to a "managed" class as a member. Instead, try something like this (note the added ref keyword in class declaration):
1 2 3 4
ref class Building {
public:
EventHandler^ eventHandler = gcnew EventHandler();
};
That is what I have done if you see my code. I commented out = gcnew EventHandler();
as it did nothing.
How does this do "nothing"? It creates a new managed object, of type EventHandler, and it assigns the handle to that new object to the variable eventHandler of type gcroot<EventHandler^>. IMO, that's not nothing.
The error was also in the
compare
file as I wrote at first.
What is a "compare" file? It's entirely unclear to me what you mean...
First, share the complete text of the error message, verbatim.
In particular,
the managed nullptr type cannot be used here
is not the complete text of the error message.
Second, share the smallest complete program you can find which exhibits the error. A user should be able to copy, paste, and compile to receive the same error.
compare is part of the C++ standard library. The C++ standard library is heavily tested, so the problem's root cause is more likely to be in some of your code that attempts to use the standard library incorrectly. There is probably enough information to help you locate the issue in the complete text of the error.
So? What is the question or problem? The code from your last post compiles for me just fine.
However, in that code, an object of type EventHandler is never created.
This declares a variable eventHandler to hold a handle to a "managed" EventHandler object in a "native" class:
1 2 3 4
class Building
{
public:
gcroot<EventHandler^> eventHandler;
...though eventHandler is never assigned to anything and thus remains a "null" handle !!!
Maybe try this:
1 2 3 4 5 6 7 8 9 10
class Building
{
public:
gcroot<EventHandler^> eventHandler;
Building()
:
eventHandler(gcnew EventHandler()) // <-- initialize field eventHandler in constructor!
{
}
};
As an aside: Consider separating the declaration and the implementation of your classes.
Building.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef BUILDING_SYSTEM_H_
#define BUILDING_SYSTEM_H_
#include <vcclr.h>
class Building
{
public:
gcroot<EventHandler^> eventHandler;
Building(void);
void someFunction(int foo);
};
#endif
using _Literal_zero = decltype(nullptr);
is giving the error in the "compare" file. I do not include this file. I am using C++ 20. I have /clr and I get another error saying /RTC1 and /clr command line options are incompatible. Hopefully this is enough information to replicate it. I am also using vs 2022. I changed my project from vs 2019 to vs 2022.
Edit: I changed to c++ 17 and it removed all the errors. Although, I still get the incompatible command line options.