mercredi 19 février 2020

How to pass a template class an argument to a function?

I'm trying to pass a template class as argument to a function.

My template class is defined as such:

template<char inputPort, int inputPin, char sensePort, int sensePin, int chan>
class PowerSwitch
{
    //...
public:
    //...
    PowerSwitch(struct Pin<inputPort, inputPin> &InputPort, struct Pin<sensePort, sensePin> &SensePort, 
        int FaultThreshold) : channel(chan)
    { ... }

}

I've instantiated this class as such (this part works):

PowerSwitch<'C', INPUT_PIN, 'C', SENSE_PIN, 0> powerChannel(powerSwitchInputPin, sensePin, 0);

Where the problem starts: I'm now trying to pass powerChannel as an argument to a function called latestSenseSMA(), which I've tried implementing as seen below, and a prototype was placed in a header file:

template<char inputPort, int inputPin, char sensePort, int sensePin, int chan>
double latestSenseSMA(PowerSwitch<inputPort, inputPin, sensePort, sensePin, chan> &powerChannel)
{
   //...
}

I'm calling it like so: int sensePinValue = latestSenseSMA<'C', INPUT_PIN, 'C', SENSE_PIN, 0>(powerChannel);.

The problem is that I get several compilation errors, including:

  • PowerSwitch was not declared in this scope,
  • expression list treated as compound expression in initializer [-fpermissive],
  • 'latestSenseSMA<'C', 2, 'C', 1, 0>' cannot be used as a function, and
  • previous non-function declaration 'template<char inputPort, int inputPin, char sensePort, int sensePin, int chan> double latestSenseSMA<inputPort, inputPin, sensePort, sensePin, chan>' CANDuinoPrototype.

Questions: How do I do actually pass a template class as a argument to a function? Is there a better way to do this?

Aucun commentaire:

Enregistrer un commentaire