c++ help?

Status
Not open for further replies.

thomen

Well-known member
Sorry I'm a total newb to c++ and boy does it have some interesting error messages compared to java and c# :)

I have the following: where RainMaker is a public property on a
Code:
class WeatherCloud : public Stepper
{
    public:
  WeatherCloud(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, void (*callback)())
    :Stepper(20, pin1, pin2, pin3, pin4)
    {
        OnComplete = callback;
        stepperStatus = STOPPED;
        TotalSteps = STEPS;
    }
    
  StepperStatus stepperStatus; //Direction to run the motor
  RainMaker rainMaker;
  void(*OnComplete)(); //completion callback
  
  private:
  unsigned long lastUpdate; //last update of position
  uint16_t TotalSteps; //Total number of steps
  uint16_t Index; //current step within pattern
  byte bottomHallSensorState;   // hall sensor state
  volatile int topStepperSwitchState = 0; //volatile because is triggered by an interrupt as the motor was straining
  
  public:
    void Initialise() {
      Serial.println("--Init CLOUD--");

      //Hall Setup For Stops
      pinMode(BOTTOM_HALL, INPUT); 
      pinMode(TOP_SWITCH, INPUT);

      //Rain peristaltic pump relay init
      pinMode(PUMP_PIN, OUTPUT);
      
      bottomHallSensorState = digitalRead(BOTTOM_HALL);
      // The screw pitch is 0.5mm so total length is 80mm, so 160 revolution is the total length of the stepper shaft.
      while (bottomHallSensorState == HIGH)
      {
        step(-20 * 40);
        bottomHallSensorState = digitalRead(BOTTOM_HALL);
        Serial.println("--no hall found--");
      }
      stepperStatus = STOPPED;
      Serial.println("--End Init CLOUD--");

      Serial.println("--Init Pump--");
      rainMaker.Initialise(&RainComplete);
      Serial.println("--End Init Pump--");
    }

    void RainComplete() {
      Serial.println("--RAIN COMPLETE--");
    }
};

Rain maker looks like so:
Code:
class RainMaker
{
  public:
    void(*OnComplete)(); //completion callback
    int pumpState = LOW;
  private:
    long lastTimePumpCameOn = 0; //Store the last time the pump was updated
    const long pumpInterval = 2000;
   
   public:
    RainMaker() {
    }

    void Initialise(void(*callback)()) {
      digitalWrite(PUMP_PIN, pumpState);
      OnComplete = callback;
    }

    void MakeItRain() {
      lastTimePumpCameOn =  millis();
      pumpState = HIGH;
      digitalWrite(PUMP_PIN, pumpState);
    }

    void Update() {
      unsigned long currentMillis = millis();
      //If it is raining
      if(pumpState == HIGH) {
        if(currentMillis - lastTimePumpCameOn > pumpInterval) {
          pumpState = LOW;
          digitalWrite(PUMP_PIN, pumpState);
          // save the last time the pump was turned off
          lastTimePumpCameOn = currentMillis;   
          OnComplete();
        }
      }
    }
};


Unfortunately I'm getting an error:
error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&WeatherCloud::RainComplete' [-fpermissive]

rainMaker.Initialise(&RainComplete);


I was wondering the best way to correct this?
I'd prefer only the cloud knows about the rain etc but I am new to c++ syntax which is throwing me..

Thanks :)
 
You can't use a member function as function pointer. A function pointer only contains an address to call. To call a member function, the function address and an class instance pointer is required.

You can use static member functions, which can be used for function pointers. Or you could create some callback class with virtual callback methods and inherit from that.

There are other ways, like using std::function instead of a plain function pointer. However, overhead is much higher than using a virtual member function.
 
You can't use a member function as function pointer. A function pointer only contains an address to call. To call a member function, the function address and an class instance pointer is required.

You can use static member functions, which can be used for function pointers. Or you could create some callback class with virtual callback methods and inherit from that.

There are other ways, like using std::function instead of a plain function pointer. However, overhead is much higher than using a virtual member function.


Thanks very much for a comprehensive answer.. I'll adjust my code.. To probably just use a static member function
 
Status
Not open for further replies.
Back
Top