I understand that ROBOTC is based of C programing, but dose it have the ability to make classes? I am asking this because i have tried to construct a class and "class" was undefined. Is there a ROBOTC include file to overcome this problem? i assume ROBOTC can make classes because class is highlighted like any other keyword and i am just having technical problems but i just want to make sure of it. Thank you for your response!

_________________Palm Harbor University High School, Robotics team Pandara #506

First of all, standard C, as a language, does not even have such a thing as a class. The class was introduced in C++, which was originally called C with Classes; thus, there is no class in C. There does exist the struct in C and RobotC but this is not the same thing as a in C++, C#, Java, etc. The C struct is simply a wrapper for a collection of basic data types or other structs. C structs can not contain functions and there is no such thing as inheritance (that I know of), overloading, etc. C structs also don't support static or constant members, or at least RobotC structs don't

Structs can be created using the typedef keyword, thusly,

Code:

typedef struct{ int foo int bar}TMyStruct

The name for the struct is placed at the end rather than the beginning of the definition. In this regard RobotC dosen't really follow the rules.

For those unfamiliar with object oriented programming, here is how you could use a struct.A struct is essentially your own data type; you create an instance of it just like you create a standard variable,

TMyStruct instance; //Create an instance of your structinstance.foo = 0; //Member fields are accessed by placing a . after the instance name.instance.bar = 0;instance.x = false; //It is a good idea to give your instance default values. In a C++ class this would be done automatically if so designed.void fun1(){ if(instance.x) //You can use member fields in conditionals... { for(int i=0; i < instance.foo; instance.bar++) //In loops... { int a = instance.foo; //and can be manipulated just like other variables. } }}

Now RobotC does seem to define the class keyword, but I can't get it to work even using the typedef keyword. I'm not sure what to make of it as C isn't even supposed to define the class keyword. I'd stay away from it either way as you can't use the benefits of object orientation in C nor does the NXT support dynamic memory managagement, i.e. pointers; additionally RobotC doesn't seem to completely follow the rules of C for creating structs. Unfortunately C structs, in my C++ biased opinion, aren't very useful so it's ok to not use them, but if you want to knock yourself out.

_________________Captain, Head programmer, School of the Arts, Silverbots Robtics Team #2890

Actually, structs can be pretty useful in RobotC due to another limitation of RobotC....no pointers.But fortunately you can have function parameters pass-by-reference.

So you can get some of the benefits of classes by having a bunch of functions (ie what would be the member functions of the class) all take a parameter which is a reference to a structure type (which makes up the data members of the class).

Sadly I don't know of any trick to do inheritance with just structs unless you have real pointers too. But that's probably not the most important thing that RobotC is lacking.

For those looking for something a little more advanced that structs can do check this out. With everything RobotC doesn't do I was shocked that this works...but it seems to.

There is also a C device known as a union that can be combined with an annonymous struct to give 2 simultaneous ways of accessing the same data. "Why bother" you ask...

now you can either access them as "mysonar.arr[0]" or "mysonar.FL". If you need to do something in a loop the array access is easier. If you are using them in a conditional statement, the variable access is cleaner.

I know you could do the same thing with just an array with constant names for the index...but then you run into another bug of RobotC which doesn't allow arrays to be passed by reference to a function. But here we can pass the whole variable mysonar by reference into any function that needs to use or supply sonar values.

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum