Error: 'num_2' does not name a type

Hi folks,

Baby steps to PlatformIO for Arduino development. Let’s start with a simple question:

I think you gonna be surprised but the following code throws an error when compiling:

#include <Arduino.h>

const int num_1 = 15;
int num_2 = 0;

num_2 = 10;

void setup() {
  // put your setup code here, to run once:

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

Error is:
src/main.cpp:6:1: error: ‘num_2’ does not name a type

num_2 = 10;


*** [.pioenvs/uno/src/main.cpp.o] Error 1

It works on Arduino IDE as it supposed to be. I think there are some differences when you code in PlatformIO comparing to Arduino IDE.

What might be the problem?

I’m not sure why it compiled in the IDE, but the statement that’s giving you an error needs to be inside a function so that it can be executed, such as setup().

1 Like

I think you’ll also be surprised that it actually doesn’t work in the Arduino IDE as listed! :open_mouth:


But yes, the statement in question on line 6 is invalid - anything other than the initial assignment should be inside a function… either setup(), loop() or some other one you write.