0

I have this

template<class PayloadType>
    class Event {
    protected:
        PayloadType payload;
    public:
        Event(PayloadType payload);
    };

This is the definition:

template<class PayloadType>
Event<PayloadType>::Event(PayloadType payload) {
    this->payload = payload;
}

And this:

class SimplePayload {
protected:
    int number;
public:
    SimplePayload(int number) : number(number) { }
    int getNumber() { return number; };
};

template<class PayloadType>
class SimpleEvent : public Event<PayloadType> {
protected:
    PayloadType payload;
public:
    SimpleEvent(PayloadType payload) : Event<PayloadType>(payload) { }
};

Trying to use it:

SimplePayload simplePayload;
SimpleEvent<SimplePayload> *simpleEvent = dynamic_cast<SimpleEvent<SimplePayload>*>(new Event<SimplePayload>(simplePayload));

And I'm getting this error:

error: member initializer 'Event' does not name a non-static data member or base class

How can I properly construct the object?

Daniel Ribeiro
  • 10,156
  • 12
  • 47
  • 79
  • Sorry, fixed it. That's what I actually had. – Daniel Ribeiro May 13 '16 at 14:24
  • 6
    Your casting makes no sense. You allocate an `Event` object instance, it's not going to be a `SimpleEvent` whatever you say. – Some programmer dude May 13 '16 at 14:25
  • 1
    `dynamic_cast` is to change the type of an existing variable. It's pretty pointless to use it in variable creation. It'll fail, too, because the argument (`new Event...`) is not the type you're casting to (`SimpleEvent...`), it's the base type. Just use `= new SimpleEvent<...>(...)` if you want a derived type, or better, a `std::unique_ptr` if you need the polymorphism or a regular non-pointer `SimpleEvent` if not. – chris May 13 '16 at 14:26
  • 3
    The base class is `Event`, not `Event` which is the name of a template. Fix the initializer list. Either way as it's been pointed out the cast makes no sense. – user657267 May 13 '16 at 14:27
  • Would you mind explaining why? I'm getting started into C++... – Daniel Ribeiro May 13 '16 at 14:29
  • where is the definition, inside cpp file or in a header? – W.F. May 13 '16 at 14:32
  • I've updated the question. Definition in .h and implementation in .cpp – Daniel Ribeiro May 13 '16 at 14:32
  • 1
    @DanielRibeiro move the implementation to a header file and all should compile and link... – W.F. May 13 '16 at 14:33
  • @WojciechFrohmberg that compiles perfectly now! Thank you! – Daniel Ribeiro May 13 '16 at 14:35
  • @DanielRibeiro read the comments above, you can't downcast an instance of a base class. – user657267 May 13 '16 at 14:37
  • Yes, that makes perfect sense. I was doing it the reverse way. Thanks! – Daniel Ribeiro May 13 '16 at 14:39

2 Answers2

2

You need to specify the template argument:

template<class PayloadType>
class SimpleEvent : public Event<PayloadType> {
protected:
    PayloadType payload;
public:
    SimpleEvent(PayloadType payload) : Event<PayloadType>(payload) { }
                                            ~~~~~~~~~~~~~
};

EDIT

For the link error, try to move the definition to the header file.

See Why can templates only be implemented in the header file?

Community
  • 1
  • 1
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
0

I see couple of errors.

Error 1

template<class PayloadType>
Event<PayloadType>::Event(PayloadType payload) {

    // this->payload is of type PayloadType*
    // payload is of type PayloadType
    // Assigning to a pointer using an object is a problem.
    this->payload = payload;
}

The easiest resolution will be to make this->payload an object of type PayloadType.

Error 2

You are not using the template parameter when using the base class constructor from the derived class.

  SimpleEvent(PayloadType payload) : Event(payload) { }

needs to be:

  SimpleEvent(PayloadType payload) : Event<PayloadType>(payload) { }

Update

The OP's question has been updated since this answer. So, the first error is not an error any longer.

R Sahu
  • 204,454
  • 14
  • 159
  • 270