Declare functions and classes in sketch

Read an old post about declaring functions in the top of the sketch to solve scope issues. (function not declared in scope - Syntax & Programs - Arduino Forum)

Example that FunctionA calls FunctionB, that will generate an error if FunctionB is below FunctionA in the code.
But by declare FunctionB in the beginning of the sketch this issue is resolved.

Example Top of sketch

void DisplayClear(Rectangle &bounds);

Later somewhere in the code

void DisplayClear(Rectangle &bounds) {
   // Clear the display rectangle
}

How can I achieve the same result with a class in the sketch?

Example class

class SensorLight: public Thread {
  private: 
        bool isDaylight;
  public:
        
        SensorLight() {
          isDaylight = false;
        }
        
        bool IsDaylight() {
          return isDaylight;
        }
        
        word GetBackgroundColor() {
          return (IsDaylight()) ? 0x801000 : 0xff3333;
        }
        
        word GetFontColor() {
          return (IsDaylight()) ? 0x000000 : 0x33ffcc;
        }
        
	void run(){
          // do some light stuff
          runned();
	}
};

So I would like to declare this in the top of the sketch. Tried this
class SensorLight {
public:
word GetBackgroundColor() {return 0xffffff;}
word GetFontColor() {return 0xffffff;}
};
SensorLight light = SensorLight();

But I'm getting the error
arduino_sketch:494: error: redefinition of 'class SensorLight'
arduino_sketch:147: error: previous definition of 'class SensorLight'
arduino_sketch:902: error: redefinition of 'SensorLight light'
arduino_sketch:152: error: 'SensorLight light' previously declared here

Example class

Where is this class defined? Where is your COMPLETE sketch?

The sketch is huge but this is example issue.

class SensorLight { 
  public: 
    word GetBackgroundColor() {return 0xffffff;} 
    word GetFontColor() {return 0xffffff;} 
};
SensorLight light = SensorLight();

// some code here

class Rectangle
{
  private:
  public:
    int x;
    int y;
    int w;
    int h;
    Rectangle(int in_x = 0, int in_y = 0, int in_w = 0, int in_h = 0) {
      x = in_x;
      y = in_y;
      w = in_w;
      h = in_h;
    }
};

class Text
{
  private:
    unsigned int charLength;
    unsigned int charWidth;
    String output;
  public:
    Rectangle bounds;
    Text(Rectangle s_bounds, unsigned int s_length, unsigned int s_width) {
      bounds = s_bounds;
      charLength = s_length;
      charWidth = s_width;
      output = "";
    }
    void UpdatePosition( int s_x, int s_y ) {
      bounds.x = s_x;
      bounds.y = s_y;
    }
    void Print(double val) {
      char tmp[charLength];
      dtostrf(val, charLength, 0, tmp);
      Print(tmp);
    }
    void Print(String val) {
      char tmp[charLength];
      val.toCharArray(tmp,charLength);
      Print(tmp);
    }
    void Print(char* val) {
        // remove old
      
      // type new text
      String o = "";
      for(int i = charLength-1; i >= 0; i--) {
         char t = val[i];
         if(t!=0) {
           o = String(t)+o;
         } else {
           
         }
      }
      output = o;
      word fcolor = light.GetFontColor();
      // print here
    }
};

// some more code

class SensorLight
{
  private: 
        static const int PinD = 4;
        static const int PinA = A3;
        int val;
        bool isDaylight;
  public:
        bool u_values;
        
        SensorLight()
        {
          u_values = false;
          isDaylight = false;
        }
        
        bool IsDaylight() {
          return isDaylight;
        }
        
        word GetBackgroundColor() {
          return (IsDaylight()) ? 0x801000 : 0xff3333;
        }
        
        word GetFontColor() {
          return (IsDaylight()) ? 0x000000 : 0x33ffcc;
        }
        
	void run(){
          
          Logger("LIGHT:Run:");
          
          int val_new = analogRead(PinA);  //Get photoresistor value
          int val_new_d = digitalRead(PinD);
          val_new /= 4;  //with a 10k resistor divide the value by 2, for 100k resistor divide by 4.
          
          // Check if daylight or nightlight, set true if swap
          u_values = true;
         
          Logger("LIGHT:ValueA:"+val_new);
          Logger("LIGHT:ValueD:"+val_new_d);
          
          bool prev = isDaylight;
          isDaylight = (val<1);
          if(prev!=isDaylight) {
            q.enqueueEvent(Events::EV_TIMER0, 0);
          }
          
          runned();
	}
};
SensorLight light = SensorLight();

// generates the errors

sketch_jun26a:77: error: redefinition of 'class SensorLight'
sketch_jun26a:3: error: previous definition of 'class SensorLight'
sketch_jun26a:128: error: redefinition of 'SensorLight light'
sketch_jun26a:8: error: 'SensorLight light' previously declared here

Could it have anything to do with the fact that HAVE defined class SensorLight twice?

declaring functions
How can I achieve the same result with a class in the sketch?

Because compiler reads from top to down all classes and functions needs to be placed in an order for the compiler to work.
If you trying to call a function before it's declared you get a scope error.
This can be solved by just declaring the function name and properties.

I'm wondering if it's possible to do the same with a class.
Guessing like a *.h file.

Maybe another example that can show what I want to accomplish.

class SensorLight { 
  public: 
    word GetBackgroundColor() {return 0xffffff;} 
    word GetFontColor() {return 0xffffff;} 
};

// some code here

class Text
{
  private:
  public:
    SensorLight* light;
    Text() {}
    
    void Print(char* val) {
      word fcolor = light.GetFontColor();
    }
};

// some more code

class SensorLight
{
  private: 
        bool isDaylight;
  public:
        SensorLight() {
          isDaylight = false;
        }
        
        bool IsDaylight() {
          return isDaylight;
        }
        
        word GetBackgroundColor() {
          return (IsDaylight()) ? 0x801000 : 0xff3333;
        }
        
        word GetFontColor() {
          return (IsDaylight()) ? 0x000000 : 0x33ffcc;
        }
};
SensorLight light = SensorLight();
Text myText = Text();
myText.light = &light;

This gives me the error

sketch_jun26a.ino: In member function 'void Text::Print(char*)':
sketch_jun26a:20: error: request for member 'GetFontColor' in '((Text*)this)->Text::light', which is of non-class type 'SensorLight*'
sketch_jun26a.ino: At global scope:
sketch_jun26a:26: error: redefinition of 'class SensorLight'
sketch_jun26a:3: error: previous definition of 'class SensorLight'
sketch_jun26a:49: error: expected constructor, destructor, or type conversion before '.' token

Why does the redefinition message surprise you? You ARE defining the class twice. Get over it.

I think the second declaration is intended to be a class definition, and the OP has been misled by some examples that define inline methods within the class declaration. The problem would be avoided by defining any inline methods in the first declaration of the class, and replacing the second declaration with definitions for all the methods/members which are not inline.

PeterH:
I think the second declaration is intended to be a class definition, and the OP has been misled by some examples that define inline methods within the class declaration. The problem would be avoided by defining any inline methods in the first declaration of the class, and replacing the second declaration with definitions for all the methods/members which are not inline.

The problem would best be avoided by doing it right, and defining the class in a header file and implementing it in a cpp file.

So the answer is 'No'.
You can't declare a class in Arduino sketch as you can with functions?

So the answer is 'No'.
You can't declare a class in Arduino sketch as you can with functions?

Wrong. You can. Whether the class definition is in the sketch file or in a separate header file does not matter. Whether the implementation is in the sketch file or in a separate source file does not matter.

What DOES matter is that you define the class ONCE. If you are planning to implement some functions in one place (in the class definition) and others in another place, you must use the scope resolution operator when implementing the methods, NOT another class statement.

So how can I declare a class for later on type the correct one? Can you give an example.

An example of how to do it with functions.

// declare the function
void DisplayClear(Rectangle &bounds);

// use the function
DisplayClear( Rectangle( 0,0,100,100) );

// here is the actual function
void DisplayClear(Rectangle &bounds) {
   // do some stuff as clear the display
}

Because compiler reads from top to down all classes and functions needs to be placed in an order for the compiler to work.
If you trying to call a function before it's declared you get a scope error.

That is not true of functions when using the Arduino IDE. Consider this

void setup()
{
  Serial.begin(115200);
  funcB();
}

void loop()
{
}

void funcB()
{
  Serial.println("In funcB");
  funcA();
}

void funcA()
{
  Serial.println("In funcA");
}

In the code as presented funcA() is called before it is defined, but it works. Behind the scenes the function prototypes are added to the program as part of the process of checking or uploading the code.

It is possible to pre-declare class names in the same way as functions

class myClass ;

void myfunction ( myClass* arg ) ; // at this point, the details of class are not known, but the compiler knows there is a class
// with that name, which can have a pointer to it.

// more stuff

// more stuff

class myClass
{
// actual class definition
}

You sometimes need to do this, if class A refers to class B and class B also refers to class A.

So how can I declare a class for later on type the correct one? Can you give an example.

I'm not 100% certain that I (or you) understand the question. But, I think that is what you want to do:

// Define the class
class SimpleClass
{
   SimpleClass();   // Constructor
   ~SimpleClass();  // Destructor

  void Function(int anArg);
};

// Implement the class
SimpleClass::SimpleClass()
{
}

SimpleClass::~SimpleClass()
{
}

void SimpleClass::Function(int theArg)
{
    Serial.print("theArg = ");
    Serial.println(theArg);
}

// Create an instance
SimpleClass simpleton;

// Use the class
void setup()
{
   Serial.begin(115200);

   simpleton.Function(42);
}

void loop()
{
}