来自外部类的C ++模板函数

时间:2020-01-09 12:29:38

标签: c++ class templates struct

我正在使用platformio为Arduino平台编写程序,并沿这些行将一些数据嵌套到结构中,这些数据驻留在类头文件(gauge.h)中

#include "Arduino.h"

class gaugeClass{
  public:
    template<typename T, typename S>
    void writeFlash( const T& dataParam, const S datVal);
    void writeThreshold( int16_t datVal );

    struct Flasht{
      struct Settingst{
        struct UnderVoltaget{
          struct Thresholdt{
            int16_t val = 2850;
            uint16_t addr = 0x4481;
          } threshold;
          struct Delayt{
            uint8_t val = 2;
            uint16_t addr = 0x4483;
          } delay;
        } underVoltage;
      } settings;
    } ;

  private:
};

在gauge.cpp中,我有一个模板函数来访问值。

#include "Arduino.h"
#include "gauge.h"

template<typename T, typename S>
void gaugeClass::writeFlash( const T& dataParam, const S datVal){
  byte addrByte2 = dataParam.addr >> 8;
  Serial.println( addrByte2, HEX );
  Serial.println( datVal );
}

void gaugeClass::writeThreshold( int16_t datVal ){
  Flasht flash;
  writeFlash( flash.settings.underVoltage.threshold, datVal );
}

然后按如下方式创建我的main.cpp程序

#include "Arduino.h"
#include <gauge.h>

gaugeClass myGauge;

void setup(){
  Serial.begin(115200);
  gaugeClass::Flasht flash;
  myGauge.writeThreshold( flash.settings.underVoltage.threshold.val );
}

void loop(){
}

但是我希望能够直接从主程序中调用模板参数,例如

myGauge.writeFlash( flash.settings.underVoltage.threshold, flash.settings.underVoltage.threshold.val );

当我添加它而不是writeThreshold函数时,编译器给出了未定义的引用错误。 我该如何不同地调用该函数? 谢谢

0 个答案:

没有答案
相关问题