Arduino C++ - 尝试实例化具有非类型模板参数的类时出错

Arduino C++ - Error when trying to instantiate class which has non-type template parameter

本文关键字:参数 出错 类型 C++ 实例化 Arduino      更新时间:2023-10-16

这是我尝试实例化"Melodie"对象的文件:

#include <Melodie.h>
Melodie<5> m(8);
void setup()
{
}
void loop()
{
}

这是"Melodie.h"文件:

#ifndef Melodie_H
#define Melodie_H
#include <Arduino.h>
#include "pitches.h"
template <int NB_NOTES>
class Melodie
{
public:
    Melodie(int pin)
    {
        // Some unimportant stuff
    }
    void addNote(int pitch, int duration)
    {
        // Some unimportant stuff
    }
    void play()
    {
        // Some unimportant stuff
    }
private:
    char notes_[NB_NOTES];
    char durations_[NB_NOTES];
    int  notePointer_;
    int  pin_;
};
#endif

我收到以下错误消息:错误:"<"标记之前的预期构造函数、析构函数或类型转换

为什么?相同的代码在Visual Studio中工作(减去arduino特定的东西)。我以为 WinAVR 支持C++?

我尝试编译您的代码 (GCC),没有问题,进行了两个小的修改。

  1. #include <Melodie.h>更改为#include "Melodie.h"
  2. 注释掉以下内容

    //#include <Arduino.h>
    //#include "pitches.h"
    

因为它们没有被使用。