the managed nullptr type cannot be used here.

I have some code that should just call an event but I get this error:
the managed nullptr type cannot be used here

I get this error in the
compare
file. Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
ref class EventHandler {
public:
	event StructureIDChanged^ OnIDChange;
	void CallOnIDChange(int i) {
		OnIDChange(i);
	}
};

class Building{
public:
    gcroot<EventHandler^> eventHandler; //= gcnew EventHandler();
};
Last edited on
This is not "normal" C++, but Microsoft's "C++/CLI" and you should probably have pointed that out.
https://en.wikipedia.org/wiki/C%2B%2B/CLI#Handles

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();
};
Last edited on
I can't do that as I have another class deriving from it. That is why I used gcroot<>.
This works for me:
1
2
3
4
5
6
7
8
9
10
11
#include <vcclr.h>

ref class SomeManagedClass {
public:
    /* ... */
};

class NativeClass {
public:
    gcroot<SomeManagedClass^> managedObject = gcnew SomeManagedClass();
};

...just as well as:
1
2
3
4
5
6
7
8
9
class NativeClass {
public:
    NativeClass(void)
    {
        managedObject = gcnew SomeManagedClass();
    }
protected:
    gcroot<SomeManagedClass^> managedObject;
};


See also:
https://docs.microsoft.com/en-us/cpp/dotnet/how-to-declare-handles-in-native-types?view=msvc-170
Last edited on
That is what I have done if you see my code. I commented out
= gcnew EventHandler();
as it did nothing. The error was also in the
compare
file as I wrote at first.
Last edited on
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...
Last edited on
Look, I compiled it with clr, adding gcnew did NOT fix the problem. And the file that had the error was called
compare
. Got it?
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef BUILDING_SYSTEM_H_
#define BUILDING_SYSTEM_H_
#include <vcclr.h>

delegate void StructureIDChanged(int);

ref class EventHandler {
public:
	event StructureIDChanged^ OnIDChange;
	void CallOnIDChange(int i) {
		OnIDChange(i);
	}
};

class Building
{
public:
	gcroot<EventHandler^> eventHandler;
	Building() { }
};
#endif 
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 

Building.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Building.h"

Building::Building(void)
:
     eventHandler(gcnew EventHandler())
{
     /*your code here*/
}

Building::someFunction(int foo)
{
     /*your code here*/
}
Last edited on
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.
Last edited on
Registered users can post here. Sign in or register to post.