Funktionspointer auf statische Klassenmethode in STL map



  • Hallo an alle,

    in der Funktion

    void Pointer::callFunction(std::string functionName)
    

    möchte ich vor dem Funktionsaufruf

    Pointer::functions[functionName]();
    

    abfragen ob diese existiert, da ich sonst das Programm "zerstöre".

    Folgende Klasse in Pointer.h

    #include<map>
    #include<string>
    #include<iostream>
    
    class Pointer
    {
    private: static std::map<std::string,void(*)(void)> functions;
    private: static int isInitialized;
    private: static int intilizeFunctions(void);
    
    // functions to be called by a string identifier
    private: static void functionA(void);
    private: static void functionB(void);
    private: static void functionC(void);
    private: static void functionD(void);
    
    public: static void callFunction(std::string functionName);
    };
    
    // initialize static class members
    std::map<std::string,void(*)(void)> Pointer::functions;
    int Pointer::isInitialized = intilizeFunctions();
    
    // define static class methods
    void Pointer::functionA(void){std::cout<<"functionA"<<std::endl;}
    void Pointer::functionB(void){std::cout<<"functionB"<<std::endl;}
    void Pointer::functionC(void){std::cout<<"functionC"<<std::endl;}
    void Pointer::functionD(void){std::cout<<"functionD"<<std::endl;}
    
    // initializes function pointer map; must be compilable
    int Pointer::intilizeFunctions(void)
    {
      functions["a"] = functionA;
      functions["b"] = functionB;
      functions["c"] = functionC;
      functions["d"] = functionD;
      return 1;
    }
    
    void Pointer::callFunction(std::string functionName)
    {
      if(functions.find(functionName) != Pointer::functions.end)
        Pointer::functions[functionName]();
    }
    

    Bei dem Ausdruck

    if(functions.find(functionName) != Pointer::functions.end)
    

    bekomme ich folgende Fehlermeldung:

    42 U:\Dropbox\Windows\Desktop\pointer\Pointer.h no match for 'operator!=' in '(&Pointer::functions)->std::map<_Key, _Tp, _Compare, _Alloc>::find [with _Key = std::string, _Tp = void ()(), _Compare = std::lessstd::string, _Alloc = std::allocator<std::pair<const std::string, void ()()> >](((const std::string&)((const std::string*)(&functionName)))) != Pointer::functions.std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = std::string, _Tp = void ()(), _Compare = std::lessstd::string, _Alloc = std::allocator<std::pair<const std::string, void ()()> >]'

    und folgenden Hinweis:

    note C:\Dev-Cpp\include\c++\3.4.2\bits\stl_tree.h:213 candidates are: bool std::_Rb_tree_iterator<_Tp>::operator!=(const std::_Rb_tree_iterator<_Tp>&) const [with _Tp = std::pair<const std::string, void (*)()>]

    Hat jemand eine Idee?

    Vielen Dank für eure Unterstützung.



  • end() ist eine Methode und keine Variable.



  • Oh. Peinlich. Danke 🙂



  • Und jetzt setzt du dich mal hin und liest dir durch, wie Zugriffs-Spezifizierer in C++ funktionieren. Und danach Namespaces. Und danach, was (void) bedeutet. Und dann vergisst du mal dein Java-Zeugs.


Anmelden zum Antworten