简体   繁体   English

在 C 中出现错误,说明在“{”标记之前不允许在此处定义函数

[英]Getting an error in C saying that function definition is not allowed here before '{' token

I made a small C program for an Arduino board to play some tones via a piezo buzzer.我为 Arduino 板制作了一个小 C 程序,通过压电蜂鸣器播放一些音调。 I adapted it try to take input from the serial monitor, iterate through the input using a for loop and play the corresponding tones using if/if else.我调整了它尝试从串行监视器获取输入,使用 for 循环遍历输入并使用 if/if else 播放相应的音调。

Unfortunately I have run into a problem that I cannot figure out as I am still quite new to C. If you have the time would you mind pointing me in the right direction as to fix it?不幸的是,我遇到了一个我无法弄清楚的问题,因为我对 C 还是很陌生。如果你有时间,你介意指出我正确的方向来解决它吗?

Thanks in advance.提前致谢。

I am getting an error:我收到一个错误:

/Assignment_02_V2.ino: In function 'void loop()': /Assignment_02_V2.ino:38:16: error: a function-definition is not allowed here before '{' token /Assignment_02_V2.ino:在函数“void loop()”中:/Assignment_02_V2.ino:38:16:错误:在“{”标记之前不允许有函数定义

int main() { ^ exit status 1 int main() { ^ 退出状态 1

Here is the code:这是代码:

#define noisyPin 11    // set pin 11 to "noisyPin"

// define notes
#define NOTE_A4 440   // set frequency of A
#define NOTE_B4 494   // set frequency of B
#define NOTE_C4 262   // set frequency of C
#define NOTE_D4 294   // set frequency of D
#define NOTE_E4 330   // set frequency of E
#define NOTE_F4 349   // set frequency of F
#define NOTE_G4 392   // set frequency of G
#define NOTE_C5 523   // set frequency to high C

// set strings for serial monitor input
String msgToUsr = "Enter your note selection (A-G). A space indicates a pause of 0.5 seconds: ";
String usrMelody;

void setup() {
    pinMode(13, OUTPUT);  // set the onboard LED to indicate when a sound is playing
    pinMode(noisyPin, OUTPUT);  // directs noisyPin (pin 3) to output
    Serial.begin(9600);
}

void loop() {
    Serial.println(msgToUsr);
    while (Serial.available()==0) { 
    }
  
    usrMelody = Serial.readString();
    melodyLength = strlen(usrMelody);
  
    int main() { 
        int i=0; 
        for (i = 1; i < melodyLength; i++) {
            if (i == c || i == C) {
                buzzer(noisyPin, NOTE_C4, 500);   // activate buzzer to play NOTE_C4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == d || i == D) {
                buzzer(noisyPin, NOTE_D4, 500);   // activate buzzer to play NOTE_D4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == e || i == E) {
                buzzer(noisyPin, NOTE_E4, 500);   // activate buzzer to play NOTE_E4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == f || i == F) {
                buzzer(noisyPin, NOTE_F4, 500);   // activate buzzer to play NOTE_F4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == g || i == G) {
                buzzer(noisyPin, NOTE_G4, 500);   // activate buzzer to play NOTE_G4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == a || i == A) {
                buzzer(noisyPin, NOTE_A4, 500);   // activate buzzer to play NOTE_A4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else  if (i == b || i == B) {
                buzzer(noisyPin, NOTE_B4, 500);   // activate buzzer to play NOTE_B4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == " ") {
            delay(500)
            }
        } 
    return 0; 
    }
} 

void buzzer(int targetPin, long frequency, long length) {
    digitalWrite(13, HIGH); // turn the onboard LED on
    long delayValue = 1000000 / frequency / 2; // calculate delay value
    // 1 second (in microseconds) divided by frequency divided into 2 (two phases in every cycle)
    long cycleCount = frequency * length / 1000; // calculate the total number of cycles
    // frequency (cycles per second) multiply by the number of seconds divded by 1 second
    for (long t = 0; t < cycleCount; t++) { // for loop to play sound based on the calculations above
        digitalWrite(targetPin, HIGH); // set pin to HIGH to play sound
        delayMicroseconds(delayValue);  // delay sound based on calculated delay value above
        digitalWrite(targetPin, LOW); // set pin to HIGH to stop sound
        delayMicroseconds(delayValue);  // delay sound based on calculated delay value above
    }
    digitalWrite(13, LOW); // turn the onboard LED off
}

You cannot have nested function in C. In your code, the main function is written inside the loop function.在 C 中不能有嵌套函数。在你的代码中,主函数写在循环函数中。

Check out Nested function in C for some extra explanation about that.查看C 中的嵌套函数以获得一些额外的解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C:函数定义中调试此错误? - How to debug this error in C: function definition is not allowed here? “错误:此处不允许 function 定义”在 C 中有大括号? - “error: function definition is not allowed here” w/ curly brackets in C? 错误:此处不允许定义函数。 如何纠正这个? - Error: function definition is not allowed here. How to correct this? 为什么我收到错误消息“s undeclared here not in a function” - Why am i getting the error saying "s undeclared here not in a function" 如何修复Xcode中的“此处不允许定义函数” - how to fix "Function definition is not allowed here" in Xcode 如何修复用 Xcode 编写的这段代码? 在主函数中,Xcode 显示错误:此处不允许定义函数 - How to fix this code written in Xcode? In the main function, Xcode is displaying the error:Function definition is not allowed here 这里不允许声明C中的错误 - Declaration not allowed here error in C 需要帮助解决涉及 c 语言中“此处不允许函数定义”的问题 - Need help solving a issue involving 'function definition is not allowed here' in c language C的新手……在未完成的函数声明上获取“ UArray2.h:19:错误:在&#39;*&#39;标记前应有预期的&#39;)&#39;” - New to C… getting “UArray2.h:19: error: expected ‘)’ before ‘*’ token” on an unfinished function declaration C 多重定义/此处首次定义错误 - C Mulitple definition/first defined here error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM