【发布时间】:2021-03-23 12:59:11
【问题描述】:
我正在开发一个 Arduino 项目(使它成为一个音乐播放器),但我遇到了一个我无法弄清楚的错误,即使在进行了可靠的 google 会话之后也是如此。这是我的代码,我去掉了不重要的东西。
我有 5 首歌曲要循环播放
//#define notes
int songLength;
int melody;
int noteDurations;
unsigned long verschil = 0;
int thisNote = 0;
double speed;
int song = 1;
void song1(melody, noteDurations) {
memset(melody, 0, sizeof(melody));
memset(noteDurations, 0, sizeof(noteDurations));
songLength = 112;
melody[] = {
//the notes of the song
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
noteDurations[] = {
//the noteduration of the song
};
}
void song2(melody, noteDurations) {
memset(melody, 0, sizeof(melody));
memset(noteDurations, 0, sizeof(noteDurations));
songLength = 72;
melody[] = {
//the notes of the song
};
noteDurations[] = {
//the noteduration of the song
};
}
void song3(melody, noteDurations) {
memset(melody, 0, sizeof(melody));
memset(noteDurations, 0, sizeof(noteDurations));
songLength = 24;
melody[] = {
//the notes of the song
};
noteDurations[] = {
//the noteduration of the song
};
}
void song4(melody, noteDurations) {
memset(melody, 0, sizeof(melody));
memset(noteDurations, 0, sizeof(noteDurations));
songLength = 105;
melody[] = {
//the notes of the song
};
noteDurations[] = {
//the noteduration of the song};
}
void song5(melody, noteDurations) {
memset(melody, 0, sizeof(melody));
memset(noteDurations, 0, sizeof(noteDurations));
songLength = 80;
melody[] = {
//the notes of the song
};
// note durations: 4 = quarter note, 8 = eighth note, etc
noteDurations[] = {
//the noteduration of the song
};
}
void loadSong(song) {
if (song == 1) {
song1(melody, noteDurations);
}
if (song == 2) {
song2(melody, noteDurations);
}
if (song == 3) {
song3(melody, noteDurations);
}
if (song == 4) {
song4(melody, noteDurations);
}
if (song == 5) {
song5(melody, noteDurations);
song = 1;
} else {
song++;
}
}
void setup()
{
//setup
loadSong(song);
}
void loop() {
//playing the song
int noteDuration = 750 / noteDurations[thisNote];
tone(13, melody[thisNote], noteDuration);
int sensorValue = analogRead(A1);
//Serial.println(sensorValue);
if (sensorValue < 200) {
speed = 0;
} else {
speed = sensorValue * 0.001;
}
speed = speed + 0.2;
Serial.println(speed);
int pauseBetweenNotes = noteDuration * 1.30 * speed;
delay(pauseBetweenNotes);
noTone(13);
if (thisNote < songLenght) {
thisNote++;
} else {
thisNote = 0;
loadSong(song);
}
}
我在声明和定义的每个歌曲函数中都收到以下错误:
101:12: error: variable or field 'song1' declared void
169:12: error: variable or field 'song2' declared void
195:12: error: variable or field 'song3' declared void
214:12: error: variable or field 'song4' declared void
251:12: error: variable or field 'song5' declared void
我真的不知道该做什么知道,我在google上找到的所有答案都是在调用函数时相关的。但错误发生在我创建函数及其内容的行。
希望有人能帮帮我
ps。我也遇到了一些其他错误,但它们与这个问题无关,无论如何我都会显示它们
287:19: error: variable or field 'loadSong' declared void
In function 'void setup()':
325:3: error: 'loadSong' was not declared in this scope
325:3: note: suggested alternative: 'long'
In function 'void loop()':
329:50: error: invalid types 'int[int]' for array subscript
330:27: error: invalid types 'int[int]' for array subscript
345:18: error: 'songLenght' was not declared in this scope
345:18: note: suggested alternative: 'songLength'
349:5: error: 'loadSong' was not declared in this scope
349:5: note: suggested alternative: 'long'
【问题讨论】:
-
C++函数定义中不能省略参数类型。
-
老实说,我在这里看不到多少 C++。也许是因为你来自不同的语言,比如 js 或 python。你不能指望 C++ 像其他语言一样简单地工作,good C++ book 将帮助你开始。
-
在 void loadSong(song) 的定义中,歌曲似乎不是一种类型。它似乎是一个整数值。除其他外,正如评论者所指出的那样。编辑:经检查,没有任何形式参数似乎是类型。
-
这能回答你的问题吗? variable or field declared void