Custom class... WHAT?

I did a lot of research to find an explanation about custom classes in Arduino. I'm getting a headache over here so maybe you guys could help me. First some code. Here is my custom class:

 class LetterData
   {
        int distance[];
        int rotation[];
   }

   LetterData sequence[26];

These arrays are filled in startup:

        //A
        sequence[0] = new LetterData();
        sequence[0].distance = new int[] {10, 10, 5, 5, 5, 5, 5, 10, 5};
        sequence[0].rotation = new int[] {0, 90, 90, 90, 180, 90, 0, -90, -90};

        //B
        sequence[1] = new LetterData();
        sequence[1].distance = new int[] {10, 10, 5, 5, 5, 8, 5, 8, 10, 13 };
        sequence[1].rotation = new int[] {0, 90, 90, 90, 180, 90, 90, -90, -90, -90};

The problem is I get the following error: sketch_mar12a:32: error: expected initializer before 'sequence'.
Is it even possible to create such a class in Arduino? I've looked at many tutorials about classes but none of them explains in a good way how to create and import these classes.

Any insight is really appreciated. Thanks in advance!

EDIT:

Here is my whole code. Its not pretty as I'm still busy converting it to work with arduino (I made a simulation with Unity first to create some idea's):

int letters[] = {};

   int distance[4];
   int rotation[4];

    float rideTimer;
    float rotationTimer;

    int seqNumber;
    int lNumber;

     float currentRotation;
     float treshRotation;

    bool right;
    bool left;
    
    int moveSpeed;
    int rotSpeed;

    int currDist;
    int currRot;

   class LetterData
   {
     public:
        int distance[];
        int rotation[];
   };

   LetterData sequence[26];

   void setup()
   {
    
      SetLetters();

     // GetSequence();
   }

   /*void GetSequence()
   {
       
       for(int i = 0; i<4;i++)
       {
           int index = letters[i]; 
         
          for(int j=0;j<sizeof(sequence[index].distance);j++)
           {
               distance.Add(num);
           }

           for(int num : sequence[index].rotation)
           {
               rotation.Add(num);
           }
       }
       
   }*/

    // Update is called once per frame
    void loop() 
    {
            if (rideTimer < distance[seqNumber])
            {
                //rideTimer += moveSpeed * Time.deltaTime;
                //transform.Translate(Vector3.forward * Time.deltaTime);
                currDist = distance[seqNumber];
            }
            else
            {
                    if (rotationTimer < rotation[seqNumber])
                        right = true;
                    else if (rotationTimer > rotation[seqNumber])
                        left = true;
                    else if (rotationTimer == rotation[seqNumber])
                    {

                        seqNumber += 1;
                        rotationTimer = 0;
                        rideTimer = 0;
                        
                    }
                    currRot = rotation[seqNumber];
                    if (right)
                    {
                      //  rotationTimer += rotSpeed * Time.deltaTime;
                       // transform.Rotate(0, rotSpeed * Time.deltaTime, 0);
                        currentRotation = rotationTimer;

                        if (rotationTimer > rotation[seqNumber] - treshRotation)
                        {
                            treshRotation = 0;
                           //!- treshRotation = Math.Abs(rotationTimer - rotation[seqNumber]);

                            seqNumber += 1;
                            rotationTimer = 0;
                            rideTimer = 0;
                            right = false;
                        }
                    }
                    else if (left)
                    {
                       // rotationTimer -= rotSpeed * Time.deltaTime;
                        //transform.Rotate(0, -rotSpeed * Time.deltaTime, 0);

                        if (rotationTimer < rotation[seqNumber] + treshRotation)
                        {
                            treshRotation = 0;
                          //!-  treshRotation = Mathf.Abs(rotationTimer - rotation[seqNumber])*1;
                            seqNumber += 1;
                            rotationTimer = 0;
                            rideTimer = 0;
                            left = false;
                        }
                    }
                //}  
            } 
	}

    void SetLetters()
    {
        //A
        sequence[0] = new LetterData();
         sequence[0].distance = new int[]{10, 10, 5, 5, 5, 5, 5, 10, 5};
        sequence[0].rotation = new int[] {0, 90, 90, 90, 180, 90, 0, -90, -90};

        //B
       sequence[1] = new LetterData();
        sequence[1].distance = new int[] {10, 10, 5, 5, 5, 8, 5, 8, 10, 13 };
        sequence[1].rotation = new int[] {0, 90, 90, 90, 180, 90, 90, -90, -90, -90};

        //C
        sequence[2] = new LetterData();
        sequence[2].distance = new int[] {10, 10, 5, 5, 10, 5, 10, 5 };
        sequence[2].rotation = new int[] {0, 90, 180, -90, -90, 90, -90, -90 };

        //D
        sequence[3] = new LetterData();
        sequence[3].distance = new int[] {10, 10, 8, 8, 10, 10};
        sequence[3].rotation = new int[] {0, 135, 90, -45,-90, -90};
    }

On a side note here; how would one create new arrays with predefined indexes to a custom class (like the 'SetLetters' function works in c#).

Is it even possible to create such a class in Arduino?

Of course it is.

First some code.

But nowhere near enough. Head on over to http://snippets-r-us.com for help with the snippets.

But, just for the record, the class definition needs to end with a semicolon.

Yes, I do it all the time. Look up how to create custom libraries. They're classes mostly.
It'll show you the basic formats.

You need a semicolon at the end of your class - I've had that headache before:

class ... {

};

Semicolon at the end of a class... MADNESS! Well, thanks for your help, it's working now. :smiley:

Semicolon at the end of a class... MADNESS!

It is an idiosyncrasy. But, I'm sure that when you develop your own language and compilers to support it that there will be few things that creep in that others will consider madness about your language, too.

You only need to be caught by this trap a dozen times or so before you remember that class definitions end with semicolons in addition to close braces.

It is an idiosyncrasy. But, I'm sure that when you develop your own language and compilers to support it that there will be few things that creep in that others will consider madness about your language, too.

You are probably right there.

Btw (in general), I've update my question with my whole code and a new question. How would one create new arrays with predefined indexes to a custom class? Im getting the following errors on A from SetLetters function:

sketch_mar12aa:127: error: expected primary-expression before ']' token
sketch_mar12aa:127: error: expected primary-expression before '{' token
sketch_mar12aa:127: error: expected `;' before '{' token

   class LetterData
   {
     public:
        int distance[];
        int rotation[];
   };

You've reserved how much space for the arrays? None. That's not very useful. You MUST define the size.

        sequence[0] = new LetterData();

sequence[0] is not a pointer to an instance. It is an object. You can't store the value returned by new (a pointer) in an object.

        sequence[0].distance = new int[] {10, 10, 5, 5, 5, 5, 5, 10, 5};

You are NOT using C#, so forget trying to use C# methodologies.

You are NOT using C#, so forget trying to use C# methodologies.

I know I'm not working with C#. I first made some simulations with Unity to create some ideas. Now im busy converting my c# code to C.

You've reserved how much space for the arrays? None. That's not very useful. You MUST define the size.

I know, but the problem is these sizes are not all the same. So I figured I cant use a predefined size as every array is not of the same size.

sequence[0] is not a pointer to an instance. It is an object. You can't store the value returned by new (a pointer) in an object.

This one shouldnt be there, I know it wont work.

So I figured I cant use a predefined size as every array is not of the same size.

Then, you'll need to use malloc() to reserve the space, and make the class member a pointer to that space. You can NOT create variable sized arrays the way you are going.

So yeah, I've been looking more carefully at libraries and I managed to understand it better now. Made my own header and and source file for LetterData. But, I still have some questions. Please bare with me as I'm not very experienced with C and arduino.
Here are my header and source codes:

Header(I've changed my code to use predefined arrays for better quality):

#ifndef letter_h
#define letter_h

#include "Arduino.h"

 class LetterData
   {
     public:
        int distance[10];
        int rotation[10];
   };

#endif

As for source, I'm not really sure if I have to put something in the constructor.
Source:

#include "Arduino.h"
#include "letter.h"

LetterData::LetterData()
{
	
}

The main problem is I can't figure out how you fill the arrays inside the LetterData class. Here is part of my sketch code that (I think) is relevant to this problem:

#include <letter.h>
   LetterData sequence[3];
  
   void setup()
   {
       sequence[0].distance[10] = {10, 10, 5, 5, 5, 5, 5, 10, 5, 0};
       sequence[0].rotation[10] = {0, 90, 90, 90, 180, 90, 0, -90, -90, 0};

   }

I know the "." extending doesn't work but I cant figure out how to create the same effect in C. How does one fill arrays in classes like the above code works in C#?

Not like that, that's for sure!

Here is one option:

const int distanceInitialiser[10] = {10, 10, 5, 5, 5, 5, 5, 10, 5, 0};
const int rotationInitialiser[10] = {0, 90, 90, 90, 180, 90, 0, -90, -90, 0};

void setup() {
    memcpy(sequence[0].distance[10],distanceInitialiser,sizeof(distanceInitialiser));
    memcpy(sequence[0].rotation[10],rotationInitialiser,sizeof(rotationInitialiser));
}

Not like that, that's for sure!
Here is one option

Thanks for your input mate. But I'm in a hurry and can't test this precisely. I did try using it as it is and I'm getting some void errors. Do I have to declare something first? If needed I'll post the exact erors as soon as possible.

If needed I'll post the exact erors as soon as possible.

Of course it's needed, along with the code that generates them.

A slight tweak should do it.

void setup() {
    memcpy( sequence[0].distance, distanceInitialiser, sizeof(distanceInitialiser) );
    memcpy( sequence[0].rotation ,rotationInitialiser, sizeof(rotationInitialiser) );
}

A slight tweak should do it.

Code:
void setup() {
memcpy( sequence[0].distance, distanceInitialiser, sizeof(distanceInitialiser) );
memcpy( sequence[0].rotation ,rotationInitialiser, sizeof(rotationInitialiser) );
}

Well, that did the trick. Thanks mate!

Of course it's needed, along with the code that generates them.

As the error was only found on the code that Tom gave me, I thought it wasn't necessary to post the exact same code again. I had to make a post with my mobile cause I was travelling at the time, so I couldn't post the errors right away.

EDIT:

I want to thank all of you guys. I couldn't have done it without your help. Everything works now, as I could print all the values inside my library. Again, thanks for the help and see you again on the next headache! Cheers! :slight_smile:

pYro_65:
A slight tweak should do it.

void setup() {

memcpy( sequence[0].distance, distanceInitialiser, sizeof(distanceInitialiser) );
    memcpy( sequence[0].rotation ,rotationInitialiser, sizeof(rotationInitialiser) );
}

Good catch, that's what I meant, just forgot to remove the [10]'s having copied and pasted from the OPs earlier post.

You can use 'new' too.

Letterdata *sequence[10];
sequence[0] = new Letterdata();

Make sure to create a destructor if you need to delete it.