I declared my enum and called it Dir. I wanted to use value of that type in my function Move(). Here's the code:
enum Dir
{
forward = 1,
backward = 2,
left = 3,
right = 4
};
void Move ( Dir direction , int distance )
{
direction = forward;
}
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Unfortunately i get the following errors:
V2.ino:23:13: error: variable or field 'Move' declared void
V2.ino:23:13: error: 'Dir' was not declared in this scope
V2.ino:23:29: error: expected primary-expression before 'int'
What's wrong?
4 Answers 4
The Arduino 1.7.x series from Arduino.org uses the old build system that is severely broken when you try and do anything more complex than simple types.
It adds prototypes to your functions to the top of the program so you don't have to get the order right, but it puts them in completely the wrong place. The result is a C++ file that looks like this:
#include <Arduino.h>
void Move ( Dir direction , int distance );
void setup();
void loop();
enum Dir
{
forward = 1,
backward = 2,
left = 3,
right = 4
};
void Move ( Dir direction , int distance )
{
direction = forward;
}
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
As you can see the prototype involving Dir
has been placed before the definition of Dir
, and so it fails miserably.
There are some hacks you can do to force it to place the prototypes after your definition, but I can't recall what they are off hand.
The simple answer is to ditch Arduino 1.7.10 and switch to Arduino 1.6.10 from Arduino.cc instead. This uses the Arduino Builder system which does things much better, including placing the prototypes in the right place.
Despite the numbering 1.7.10 is not a newer version than 1.6.10 - it is an older version renumbered. It's all part of the schism between the two halves of Arduino.
Even better is to ditch the awful Arduino IDE entirely and use one of the many far better IDEs that exist.
-
Thank you very much. What other IDE do you recommend? To be honest, I can't stand Arduino IDE, the code is really hard to read, personally I write programs in Visual Studio Code. I wish it was possible to upload to Arduino from there.mnj– mnj2016年08月07日 18:47:53 +00:00Commented Aug 7, 2016 at 18:47
-
-
-
I wouldn't recommend Arduino at all, use C/C++ directly instead. There is a plethora of IDE for C/C++ out there. If you get a real programming pod you can even single step and inspect hardware registers.Max Kielland– Max Kielland2018年09月17日 15:29:55 +00:00Commented Sep 17, 2018 at 15:29
I did not experience the problem because I usually use a Makefile instead of the Arduino IDE. I now tried with the IDE version 1.0.5 and I had the same problem as you. The fix is to explicitly declare the function before defining it:
void Move(Dir direction, int distance);
void Move(Dir direction, int distance)
{
direction = forward;
}
As Majenko points out, this seems to be a problem with the parsing of the preprocessor.
You have two options:
#include "myEnums.h"
void Move(Dir direction, int distance)
{
direction = forward;
}
void setup() {}
void loop() {}
-
1Or just use a different IDE. Thanks for your inputmnj– mnj2016年08月07日 18:56:47 +00:00Commented Aug 7, 2016 at 18:56
You have to define a new enum type:
typedef enum
{
forward = 1,
backward = 2,
left = 3,
right = 4
} Dir;
void Move ( Dir direction , int distance )
{
direction = forward;
}
without typedef
your Dir is just static enum variable, it can't be used to define other vars.
-
C++ creates typedefs of all enums and structs by default without you needing to do it manually.Majenko– Majenko2016年08月07日 18:32:35 +00:00Commented Aug 7, 2016 at 18:32
void setup(){} void loop(){}
, save for a couple of warnings about the function parameters not being used.