functors and callbacks

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

 



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?


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