0

I´ve created the following Event class:

Event.h

#ifndef EVENT_H
#define EVENT_H

#include<string>

template<typename T>
class Event
{
    public:
        T fnktptr; //Error: field ‘Event<int()>::fnktptr’ invalidly declared function type
        Event();
        virtual ~Event();
};

#endif // EVENT_H

Event.cpp

#include "Event.h"

template<typename T>
Event<T>::Event()
{
    //ctor
}
template<typename T>
Event<T>::~Event()
{
    //dtor
}
// No need to call this TemporaryFunction() function,
// it's just to avoid link error.
void TemporaryFunction ()
{
    Event<int> TempObj;
}

Now I tested it with the following code and it works:

Event<int> event;
int t = 5;
event.fnktptr = t;

But finally, I want to use it with a functor like this:

Event<decltype(consumeInt)> event;
event.fnktptr = consumeInt;

But now I get a Error in the Event.h file:

T fnktptr; //Error: field ‘Event<int()>::fnktptr’ invalidly declared function type
alexander-fire
  • 1,082
  • 4
  • 27
  • 52
  • Shouldn't `T fnktptr;` be `T * fnktptr;`? – xvan Mar 08 '16 at 18:57
  • Yes, you can't have an object of a function type, it is not allowed. – SergeyA Mar 08 '16 at 18:59
  • I tried it now with `T* fnktptr` and now I get these error: `undefined reference to Event::Event()` Error occured at `Event event;` This is my function consumeInt: `Event event;` – alexander-fire Mar 08 '16 at 19:00
  • @SergeyA: What would be a working alternative for my problem? (Store a functor in a class for further use.) – alexander-fire Mar 08 '16 at 19:03
  • You could use `decltype(&consumeInt)` instead. – Kurt Stutsman Mar 08 '16 at 19:04
  • @AlexMallinger, you can store a pointer to function or an `std::function` object. The latter has benefits over function pointer. – SergeyA Mar 08 '16 at 19:04
  • @KurtStutsman, I tried your code with `T* fnktptr` and get these error: `cannot convert int() to int(**)() in assigment`. If I try it with `T fnktptr` I get `undefined reference to Event::Event()` – alexander-fire Mar 08 '16 at 19:11
  • 1
    1) You can't have a member of type function. 2) When you fixed that to a pointer, you have to define templates in the header file. – Barry Mar 08 '16 at 19:12
  • @Barry, how should I fix this to a pointer? Could you give me an example? 2) I use a workaround to split implementation and declaration with the `TemporaryFunction()` – alexander-fire Mar 08 '16 at 19:16
  • @AlexMallinger Using `decltype(&consumeInt)` works when you leave it as `T fnktptr`. Since you were using it to store an `int` before you will have to leave it as your initial design. – Kurt Stutsman Mar 08 '16 at 19:20
  • @KurtStutsman: I don´t wanna store the int result. I wanna store a pointer to the function. – alexander-fire Mar 08 '16 at 19:22
  • @KurtStutsman, But if I try it with my initial code I get `undefined reference to Event::Event()` with `decltype(&consumeInt)`. – alexander-fire Mar 08 '16 at 19:25
  • I wanna store instances of the `Event` in a vector. In the next step, I would pass the vector of `Events` to a Function `run()` in that I will call `fnktptr` – alexander-fire Mar 08 '16 at 19:30

1 Answers1

2

Based on your latest comments, I would say a std::function<> is the best way forward. The example code below assumes you just want to call fnktptr and don't care about the result.

#include <functional>

class Event
{
    public:
        std::function<void ()> fnktptr;
        Event();
        virtual ~Event();
};

If you need to capture the result, you will not be able to arbitrarily have different returns for each Event object. You either must pick one or use something like boost::variant<>.

template<class T>
class Event
{
    public:
        std::function<T ()> fnktptr;
        Event();
        virtual ~Event();
};

This would allow you to create Event<int> objects for instance to use with consumeInt().

Example:

Event<int> event;
event.fnktptr = consumeInt;
Kurt Stutsman
  • 3,994
  • 17
  • 23