1

I have this typedef to define a function pointer.

typedef Script*(*CreateObjectFn)(TiXmlElement* node);

I've created a generic container for my purpose that acts like a map. It's called Dictionary, so I've created a Dictionary of CreateObjectFn inside a class like this:

class ScriptBank
{ 
public:


~ScriptBank();
    static Dictionary<CreateObjectFn>& getInstance()
    {   
       static Dictionary<CreateObjectFn>& registry = m_registry;
       return registry;
    }

private:
    static Dictionary<CreateObjectFn> m_registry;
    ScriptBank();
};

I have various type of Script so in this Dictionary I want to put inside a certain function of the derived Script.

Now from the template class:

template <class T>
class Register
{
public:
    Register()
    {
        ScriptBank::getInstance().insert("Some string", T::create);
    }
    ~Register()
    {
    }
};

Inside the derived Script i have something like:

    static Script* create(TiXmlElement* node);
static Register<DoorChStateScript> m_Registry;

My purpose is to insert the function pointer at compilation time, but compiler seems to not recognize the right type. In the Register class I got this error: cannot convert parameter 2 from 'Script *(TiXmlElement *)' to 'CreateObjectFn *'

Any ideas?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
plafone
  • 43
  • 1
  • 3
  • When you say you created a container that acts like a map, what's wrong with `std::map`? – chris Jul 24 '13 at 08:12
  • Nothing wrong. I know, its better to use STL. Its a project for school and one of the requirements were to write our own container and dont use STL. – plafone Jul 24 '13 at 08:16
  • You need to take the address of `T::create` in your `insert()` function: `insert("foo", &T::create)` – arne Jul 24 '13 at 08:25
  • Just tried, same error as before... – plafone Jul 24 '13 at 08:27
  • 2
    Read the error message carefully: "can't convert to `CreateObjectFn*`" (note the asterisk). You've declared a *pointer to a function pointer* somewhere and you're trying to initialize it with a function pointer. It'll be hard to point where exactly without an [SSCCE](http://www.sscce.org/) – jrok Jul 24 '13 at 08:27
  • The Dictionary internally treats the pointer of the template parameter. Thats why I have a pointer to a function pointer. – plafone Jul 24 '13 at 09:08
  • Then find where you are using the `CreateObjectFn*` (i.e. `T*` in your template) and passing a non-pointer (i.e., a `T`). The error message should tell you where that is, maybe buried in several lines of template error messages. But it's always good to know how to read those errors. – Arne Mertz Jul 24 '13 at 09:13
  • I've found the solution for the problem overloading the insertion function of my data structure and the insertion is working pretty well. Now I have this issue: in insertion phase the address of the function pointer is valid, but after searching it points to a non valid area. Thats something wrong with the static function that points to? Ideas? – plafone Jul 27 '13 at 12:17

0 Answers0