【问题标题】:error: variable or field *** declared void错误:变量或字段 *** 声明为 void
【发布时间】: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

标签: c++ arduino


【解决方案1】:

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

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

【讨论】:

  • 当我尝试对歌曲 1 执行此操作时,出现更多错误:103:35: error: invalid conversion from 'int' to 'void*' [-fpermissive] 25:0, 1: 235:14: note: initializing argument 1 of 'void* memset(void*, int, size_t)' 104:49: error: invalid conversion from 'int' to 'void*' [-fpermissive] 25:0, 1: 235:14: note: initializing argument 1 of 'void* memset(void*, int, size_t)' 106:10: error: expected primary-expression before ']' token 138:15: error: expected primary-expression before ']' token
  • @JelleDriessen 因为您的代码几乎每一行都被破坏或毫无意义。我的意思是没有冒犯,这就是它应该诚实的方式。您在正确完成第 1 步之前开始了第 10 步。拿起一本好的 C++ 书籍并重新开始可能是个好主意。不要一次写这么多代码。一次只写一小段,然后编译它,如果你遇到任何错误,你必须理解并修复它们,然后再继续下一个小步骤。 C++ 真的很难学,尤其是如果你在没有指导的情况下深入学习。
猜你喜欢
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 2013-11-25
  • 1970-01-01
相关资源
最近更新 更多