类中的C ++ var没有命名类型

时间:2016-08-16 08:34:32

标签: c++ arduino

我知道这个问题已被多次询问,但我找到的答案都没有能够帮助我。我正在尝试构建std::vector并使用structs填充它。我想让这些变量变为静态和常量,因此它们可以轻松传递。我现在的代码是:

Melodies.h

#ifndef Melodies_h
#define Melodies_h

#include "Arduino.h"
#include <StandardCplusplus.h>
#include <vector>

struct Note {
  int note;
  int duration;

  Note(int a, int b) : note(a), duration(b) {}
};

struct Melody {
  std::vector<Note> notes;

  void addNote(Note note) {
    notes.push_back(note);
  }
};

const Melody NONE;
const Melody BILL;
const Melody COIN;
// this gives an error
//COIN.addNote(Note(NOTE_C4, 5));

#endif

Melodies.cpp

#include "Melodies.h"
#include "Notes.h"

// this gives an error
//COIN.addNote(Note(NOTE_C4, 5));

我收到了错误:

  

错误:&#39; COIN&#39;没有命名类型

如何存储此类变量并将其设置为我想在begin函数中执行的一次?我没有使用标准的c ++ - 这是在使用StandardCplusplus库的Arduino上。

1 个答案:

答案 0 :(得分:0)

将对象声明为const时,只能在初始化时修改其成员值。如果您可以在程序中的任何位置使用addNote(),那么COIN对象将不会是常量,是吗?

你得到的错误似乎令人困惑,但尝试使COIN非const,或者添加一个构造函数,这将允许你在初始化时填充注释向量。