Eric111
Eric111

Reputation: 39

C++ delegate, interface and ref are error type

I'm working on a project, and trying to implement events for it.

We are using SDL2 and created a new file for this event system.

The problem is that visual studio marks items with delegate, interface or ref as errors. Saying that they are error-type. Using namespace System didnt work, but I'm having trouble understanding what the problam really is. Why arent they recognized?

I wrote this referencing this page: https://learn.microsoft.com/es-es/cpp/dotnet/how-to-use-events-in-cpp-cli?view=msvc-170#virtual-events

Here's the code:

namespace ev_sys {

    struct event_info {
        int irrelevant_test_data;
    };

    delegate void Del(int, float);

    //Inherit from event_receiver in order to be able to consume events
    class event_receiver {
    public:
        virtual void handler(event_info&) = 0;
    };
    //To create new functions inherit from event_receiver
    //and override handler


    interface struct event_triggerer_interface {
    public:
        event Del^ E;
        void fire_event(event_info&);
    };

    //Obligatoriamente ha de crearse con gcnew por ser "ref"
    ref class event_source : public event_triggerer_interface {
        virtual event Del^ E;
        virtual void fire_event(event_info& e) {
            E(e);
        }
        void suscribe(event_receiver er) {
            E += gcnew Del(er, &event_receiver::handler);
        }
        void desuscribe(event_receiver er) {
            E -= gcnew Del(er, &event_receiver::handler);
        }
    };
}

Thanks in advance

Upvotes: -3

Views: 28

Answers (0)

Related Questions