Problem mit Funktions Pointer
-
Das cpp File der Klasse zuerst:
CMathFloat::CMathFloat(){ float(CMathFloat::*ptr)(float); string muh = "sin(x)"; regFunction(muh,SinX); } void CMathFloat::regFunction(string functionName, float (CMathFloat::*ftp)(float)){ m_regFunctions.insert(make_pair(m_mapIndex,functionName)); m_pointRegistered.insert(make_pair(functionName,ftp)); m_mapIndex++; //berechne die Funktion } float CMathFloat::SinX(float Eingabe){ const float PI = 3.1415926535; Eingabe = Eingabe / 180 * PI; float Zahl = (float) sin(Eingabe); return Zahl; } float CMathFloat::berechneTeilFunktion(){ (... blabla ...) endErgebnis = m_pointRegistered[m_regFunctions[i]](ergebnis1); (... blabla ...) }Und der Header:
#ifndef CMATHFLOAT_H_ #define CMATHFLOAT_H_ #include <string> #include <map> using namespace std; class CMathFloat{ private: map<int,string> m_regFunctions; map<string,float (CMathFloat::*const) (float)> m_pointRegistered; int m_mapIndex; string m_eingabe; float m_ergebnis; float addOperation(float Wert1, float Wert2); float subOperation(float Wert1, float Wert2); float multOperation(float Wert1, float Wert2); float transformiereStringToFloat(string Eingabe); void entferneLeerstellen(); int StringToInt(string Eingabe); string IntToString(int Wert); float berechneErgebnis(); bool handleFunktionen(); bool checkFunctions(); bool checkZeichen(char zeichen); float SinX(float Eingabe); float berechneTeilFunktion(); public: CMathFloat(); ~CMathFloat(); void setEingabe(string eingabeString); float starteBerechnung(); bool checkNumeric(); bool checkEingabe(); void regFunction(string functionName, float (CMathFloat::*ftp)(float)); };Ok der Code oben funktioniert NICHT. Ich bin mit meinem Latein am Ende und versteh nicht was ich falsch mache.
Zum verstehe: Ich will im Header diverse Funktionen adden die alle das Format
"float Name (float zahl)" haben:
regFunction(muh,SinX);Die Speichert das Programm in einer map ab. Mit einem Namen und dem Pointer auf die Funktion.
m_regFunctions.insert(make_pair(m_mapIndex,functionName));
m_pointRegistered.insert(make_pair(functionName,ftp));Später will ich dann die Funktion ganz normal aufrufen über den pointer.
endErgebnis = m_pointRegistered[m_regFunctions[i]](ergebnis1);Kann mir jemand erklären was ich falsch mache?
-
"Geht nicht" ist keine Fehlerbeschreibung. Weiters wäre es guter Ton, ein (bis auf das Thema der Frage) lauffähiges Minimalbeispiel zu zeigen.
Methodenpointer funktionieren so:
#include <iostream> #include <map> #include <string> class foo_t { public: typedef float ( foo_t::*method_ptr )( float ); private: typedef std::map< std::string, method_ptr > method_map_t; method_map_t method_map; public: void register_method( const std::string &name, method_ptr method ) { method_map.insert( std::make_pair( name, method ) ); } float bar( float f ) { std::cout << "foo_t::bar( )" << std::endl; return 0.; } void doit( const std::string &name ) { method_map_t::iterator i = method_map.find( name ); if( i != method_map.end( ) ) { ( this->*( i->second ) )( 0. ); } } }; int main( ) { foo_t foo; foo.register_method( "bar", &foo_t::bar ); foo.doit( "bar" ); }cheers, Swordfish
PS: Darüber gibt's AFAIR nen ziemlich guten Artikel auf CodeProject.
-
Danke für die Antwort! Werd das gleich probieren
"Geht nicht" ist keine Fehlerbeschreibung. Weiters wäre es guter Ton, ein (bis auf das Thema der Frage) lauffähiges Minimalbeispiel zu zeigen.
Werd es nächstes mal besser schreiben. Sorry