Arduino raise the error: `does not name a type` when an Object is used outside of the main two blocks setup and loop
As you have already noticed, you cannot call methods of a class, nor access its properties, outside a "block" (as you call it).  More specifically, you can only perform "programatical" operations from within a function.  Anything outside a function (known as the global scope) is purely for declaration and initialization of variables and types, etc.

Any class variables declared in the global scope have their constructor executed before the rest of the program (even before setup) gets run.  However, setup is not the first function to be called at startup.

Anything you put into a constructor has to take into account the fact that anything might be happening between the constructor being called and the object instance actually being used.  Things like pinMode and digitalWrite are unsafe to use in a constructor, since there is a chance they may be negated during the startup.  That is why the majority of Arduino classes have a .begin() method which is called from setup().

The proper method for using classes with pin control and similar operations is:


The constructor takes the pins to use
The constructor saves those pins in variables
The begin() method is called in setup()
The pins are configured appropriately.


For example:

class myClass {
    private:
        int _myPin;
    public:
        myClass(int pin) : _myPin(pin) {}

       void begin() {
            pinMode(_myPin, OUTPUT);
            digitalWrite(_myPin, HIGH);
        }
};

myClass p(4);

void setup() {
    p.begin();
}

void loop() {
}


The object instance p is in the global scope and can be referenced from any function. The pin is configured only when setup() is run.

The startup sequence, more specifically, is typically this:


The memory used by global and static variables is "zeroed".
Any pre-defined variable values (BSS) are copied from flash into RAM.
Any object constructors are called.
The main() function is called (main startup function).
The init() function is called which configures the Arduino platform.
The initVariant() function is called which performs any board specific setup routines.
USB is configured on the boards that have it
Your setup() function is called.
Your loop() function is called.
The serialEvent() system runs if defined.
Go to 10.


As you can see quite a lot happens between the constructor being called and setup() being called, and you cannot reliably predict what that might be.  Some boards may do nothing more than configure interrupts and enable timers (for things like delay and millis), but other boards may configure IO pins to default settings (especially more complex boards with multiple functions per IO pin), so really the only safe operations you can perform in a constructor are operations which act directly on the internals of the class itself.  Anything outside the scope of the class could be changed before you come to use it.