Problem mit Funktionszeiger



  • Hi,

    ich habe folgendes Problem. Als erstes gibt es eine Klasse Vector:

    template <class T> 
    class Vector {
    
      public:
        typedef T value_type;
        typedef unsigned int size_type;
    
        typedef T* iterator;
        typedef const T* const_iterator;
        typedef vector<T> subst_type;
    
      private:
        size_type _rows;
        T* _data;
        void resize(size_type i) {_rows=i; }
    
      public:
    
        // constructors/destructors
        ...
    
        // initialize Elements
        ...
    
        // Vector-dimensions 
        ...
    
        // Access to elements
        ...
    
        // Vector-Vector Operators
        ...
    
        // I/O - Operators/Methods
        ...	
    
        // Iterators
        ...
    
        // modifying access to rows/colums
        ...
    
        // Elementweise bestimmte Funktion durchfuehren
        Vector<T>& ForEachElement(T (*pt2Func)(T)) { 
            for (size_type i=0;i<_rows;i++) 
                { (operator[](i))=pt2Func(operator()(i)); }
            return *this;
            }
    
        };
    

    Die Funktion ForEachElement soll also einen Funktionszeiger bekommen und diese Funktion für jedes Element ausführen. Das ganze kann man also verwenden, um zum Beipsiel verschiedene Vektor-normen zu berechnen:

    template<class T> inline T AbsoluteNorm(Vector<T> v) 
    {v.ForEachElement(&absolute<T>); return accumulate(v.begin(),v.end(),(T)0);}
    

    Und an anderer Stelle:

    template<class T> inline T absolute (const T a)   	
    {return (a<(T)0) ? -a : a;}
    

    Letzlich rufe ich irgendwo auf:

    Vector<double> pp;
    ...
    if (AbsoluteNorm(pp)!=1) Normalize(pp);
    

    und erhalte die Fehlermeldung:

    ...\latools.h(46) : error C2664: 'class Vector<double> &__thiscall Vector<double>::ForEachElement(double (__cdecl *)(double))' : cannot convert parameter 1 from 'double (const double)' to 'double (__cdecl *)(double)'
    None of the functions with this name in scope match the target type
    ...\stattools.cpp(654) : see reference to function template instantiation 'double __cdecl AbsoluteNorm(class Vector<double>)' being compiled

    😮 Was will der Verfasser mir damit sagen? Ach übrigens: Alles unter MS VC++ 6.0, unter Linux-gcc 3.2 läuft es Problemlos. Wieso?

    Danke im Vorraus 😉



  • [vermutung]
    absolute ist eine Funktion vom Typ T (const T)
    ForEachElement erwartet aber eine Funktion vom Typ T (T)
    Kann es sein, dass das deinen Compiler streiken lässt?
    [/vermutung]



  • [Bestätigung]
    Yip, das wars! Danke.
    Dann hätte der Linux-gcc aber eigentlich auch meckern müssen.
    [\Bestätigung]



  • Kannst ja mal den GCC updaten, Version 3.2 ist doch schon etwas älter. Wobei ich mir nicht sicher bin, ob das vielleicht doch ein Problem des MSC 6 ist. DER ist ja richtig alt, und non-const -> const Umwandlungen sind eigentlich vom Compiler implizit durchführbar.


Anmelden zum Antworten