arduino / arduino-cli

Arduino command line tool
https://arduino.github.io/arduino-cli/latest/
GNU General Public License v3.0
4.24k stars 371 forks source link

Enum defined after a function template cannot be compiled #2525

Closed Silver-Fang closed 5 months ago

Silver-Fang commented 5 months ago

Describe the problem

C:\Users\vhtmf\AppData\Local\Temp\.arduinoIDE-unsaved202414-28316-jorhob.tu2i\sketch_feb4a\sketch_feb4a.ino:8:1: error: 'E' does not name a type
 E D()
 ^
C:\Users\vhtmf\AppData\Local\Temp\.arduinoIDE-unsaved202414-28316-jorhob.tu2i\sketch_feb4a\sketch_feb4a.ino:8:3: error: expected initializer before 'D'
 E D()
   ^

exit status 1

Compilation error: 'E' does not name a type

To reproduce

template<typename T>
void C(T F)
{}
enum E
{
  OS
}
E D()
{
  return E::OS;
}
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Expected behavior

I have verified this code in MSVC, and there should be no problem.

Arduino IDE version

版本:2.2.1 日期:2023-08-31T14:35:44.802Z CLI 版本:0.34.0 Copyright © 2024 Arduino SA

Operating system

Windows

Operating system version

版本 Windows 11 专业版 Insider Preview 版本号 23H2 安装日期 ‎2024/‎1/‎28 操作系统版本 26040.1000 体验 Windows Feature Experience Pack 1000.26040.1000.0

Additional context

No response

Issue checklist

per1234 commented 5 months ago

Hi @Silver-Fang. Thanks for taking the time to submit an issue.

I see we have another report about this at https://github.com/arduino/arduino-cli/issues/1269.

It is best to have only a single issue per subject so we can consolidate all relevant discussion to one place, so I'll go ahead and close this in favor of the other.


As I mentioned in the other issue thread, the workaround is to manually add the function prototype at the appropriate location in the sketch, after the definition of the E type:

enum E
{
  OS
};

E D();  // Function prototype

E D()
{

I see there are also several bugs in your code that are simply a matter of it being invalid rather than anything caused by a defect or limitation in the Arduino sketch build system. So you will find the sketch still won't compile even after adding the prototype. If you would like assistance with those other problems, you are welcome to post over on Arduino Forum. I'm sure we will be able to help you over there:

https://forum.arduino.cc/c/using-arduino/programming-questions/20