首页 > 解决方案 > 错误:变量或字段 *** 声明为无效

问题描述

我正在开发一个 Arduino 项目(使它成为一个音乐播放器),我有一个我无法弄清楚的错误,即使在一个可靠的谷歌会话之后。这是我的代码,我去掉了不重要的东西。

我有 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上找到的所有答案都是在调用函数时相关的。但错误发生在我创建函数及其内容的行上。

我希望有人可以帮助我

附言。我也遇到了一些其他错误,但它们与这个问题无关,无论如何我都会展示它们

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++arduino

解决方案


当你声明一个函数时,你需要指定参数的类型:

void playSong(std::string songName, float songDuration){
...
}

推荐阅读