6

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?

10
  • Please post a Minimal, Complete, and Verifiable example – KIIV Aug 7 '16 at 17:22
  • Compiled OK for me, when I supplied (empty) setup() and loop() functions. There being fewer than 23 lines (the line # of the cited errors), I assume we're seeing an excerpt. Can you reduce your code to the minimum necessary to demonstrate the problem and show that, entirely? – JRobert Aug 7 '16 at 17:24
  • @JRobert I also did just like you: I created a new sketch and copied the code from my post (I didn't put anything into setup() or loop()). I got the same errors. Why do i get them? – Loreno Aug 7 '16 at 17:27
  • I second JRobert: compiled OK for me with void setup(){} void loop(){}, save for a couple of warnings about the function parameters not being used. – Edgar Bonet Aug 7 '16 at 17:33
  • I'm using Arduino IDE 1.7.10. I'll check this code on my second PC – Loreno Aug 7 '16 at 17:37
3

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.

4
  • 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. – Loreno Aug 7 '16 at 18:47
  • Well, I have to recommend UECIDE, but then I am biased since I wrote it ;) – Majenko Aug 7 '16 at 18:48
  • I'll check it out – Loreno Aug 7 '16 at 19:08
  • 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 Sep 17 '18 at 15:29
4

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;
}
0
2

As Majenko points out, this seems to be a problem with the parsing of the preprocessor.

You have two options:

  • Use Edgar Bonets solution and declare the function before defining it
  • Create a separate header file for your enum in a new tab and import it with #include "[myEnumsHeaderFilename].h"
  • #include "myEnums.h"
    
    
    void Move(Dir direction, int distance)
    {
       direction = forward; 
    }
    
    void setup() {}
    
    void loop() {}
    
    1
    • 1
      Or just use a different IDE. Thanks for your input – Loreno Aug 7 '16 at 18:56
    0

    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.

    1
    • C++ creates typedefs of all enums and structs by default without you needing to do it manually. – Majenko Aug 7 '16 at 18:32

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.