Re: functors and callbacks

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




John Russo wrote:
> Hello list,
> 
> I'm having some problems using functors to implement callbacks.
> 
> I tried to compile the following code with g++ version 4.0.1 and 3.3.6
> 
> class Functor
> {
>    public:
>        virtual void operator()(double)=0;
> };
> 
> template<class TClass>
> class SpecFunct: public Functor
> {
>    public:
>        SpecFunct(TClass* _ob,void
> (TClass::*_f)(double)):fpt(_f),object(_ob){}
>        virtual void operator()(double num)
>        {
>            (*object.*fpt)(num);
>        }
>      private:
>        void (TClass::*fpt)(double);
>        TClass* object;
> };
> 
> class TemperatureSensor {
>                      public:
>            TemperatureSensor():temperature(10.){}
>                      void get_temperature(Functor* func)
>            {
>                              if (temperature>5.)
>                    (*func)(temperature);
>            }
>    private:
>        double temperature;
> };
> 
> class StrawberryField{
>      public:
>                      StrawberryField(TemperatureSensor* pr):sensor(pr){}
>                    void water(double temp)
>        {
>            std::cout << "The temperature is " << temp <<" let's put some
> water " << std::endl;
>        }
>              void ask_sensor(Functor *func)
>        {
>            sensor->get_temperature(func);
>        }
>                private:
>        TemperatureSensor *sensor;
>   };
> 
> int main()
> {
>      TemperatureSensor sensor;
>      StrawberryField field(&sensor);
>      SpecFunct<StrawberryField> functor(&field,StrawberryField::water);
>      field.ask_sensor(&functor);
>      return 0;
> }
> 
> 
> 
> but I get the following error message:
> 
> bash-3.00$ g++ Fun_eng.cpp
> Fun_eng.cpp: In function `int main()':
> Fun_eng.cpp:69: error: no matching function for call to `
>   SpecFunct<StrawberryField>::SpecFunct(StrawberryField*, <unknown type>)'
> Fun_eng.cpp:11: error: candidates are:
>   SpecFunct<StrawberryField>::SpecFunct(const SpecFunct<StrawberryField>&)
> Fun_eng.cpp:13: error:                
> SpecFunct<TClass>::SpecFunct(TClass*,
>   void (TClass::*)(double)) [with TClass = StrawberryField]
> 
> 
> I tried the same code with the Intel Compiler and it compiles and runs
> without problems. What's wrong?
> 

The correct syntax for getting a pointer to a member function is
&StraberryField::water. I don't know the where that's found in the
Standard but I know it's there :)

With that change it works (tested with gcc4.1.0 SuSE)

MSVC++7 accepted what you used BTW and not the correct syntax. This
changed with the 2005 version.

HTH, Peter

> 
> Thanks for the help,
> 
>  John Russo

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux