我知道這個問題已被問了很多次,但沒有找到的答案能夠幫助我。我正在嘗試構建一個std::vector
並用structs
填充它。我想讓這些變量是靜態和常量的,所以它們可以很容易地傳遞。我的代碼現在的問題是:類中的C++ var沒有命名一個類型
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));
我的錯誤(S):
error: 'COIN' does not name a type
我怎麼能存儲此類型的變量並將其設置爲1次,就像我想在begin
函數中做的那樣?我不使用標準的C++ - 這是使用StandardCplusplus庫的Arduino。
[Works on gcc](http://ideone.com/JYSP1S) – StoryTeller
[MCVE]請。無論如何,你應該做一個簡單的初始化,不要使用任何'begin'函數,你的設計很奇怪。 – LogicStuff
好吧,這是在我提到的使用StandardCplusplus庫的Arduino上。如果這與標準的gcc工作正常,那麼它一定是圖書館。我會盡量讓它更簡單,然後對於Arduino – shiznatix