View unanswered posts | View active topics It is currently Mon Sep 21, 2020 12:03 pm

Reply to topic  [ 4 posts ] 
Functions with return statments inside of structs 
Author Message

Joined: Thu Dec 20, 2012 3:43 pm
Posts: 1
Post Functions with return statments inside of structs
In an attempt to avoid the very annoying "inline" error and with the goal to make cleaner code I have written a PID control loop that you can create multiple instances of (hopefully).
This part of the code successfully compiles:
typedef struct{
   float KP;
   float KI;
   float KD;
   float previousError;
   int integral;
} PidValue;

typedef struct{
   int value(int value, int targetValue, PidValue k){
      int error = targetValue - value;
      k.integral += error;
      if(error == 0){
         k.integral = 0;
      if(abs(error)> 40){
         k.integral = 0;
      int derivative = error - k.previousError;
      k.previousError = error;
      return ((k.KP*error)+(k.KI*k.integral)+(k.KD*derivative));
} PID;

PidValue testV ={0, 0, 0, 0, 0};
PID test;

This, however, fails to compile and gives errors on the indicated lines:

int returned;
task main()
   // Hooray for test cases!
   while (1){
      for(int j = 0; j<100; j++){
         returned = test.value(j, 50, testV); // See error below
      for(int j = 100; j>0; j--){
         returned = test.value(j, 50, testV); // Same error given

Here is the error that is given:
File "PID.c" compiled on Dec 20 2012 14:51:56
**Error**:'value' is not a 'struct' element of struct reference 'test'
**Error**:Unexpected token ','
**Error**:Expected->';'. Found ','
**Error**:Unexpected ',' during parsing
**Error**:Unexpected '50' during parsing
**Error**:Missing ';' before ')'
**Error**:Unexpected ')' during parsing
*Warning*:Meaningless statement -- no code generated

I'm getting the impression that I'm doing something illegal in Robot C however I'm confused why the struct will compile but when I actually try to test it it wont.

~WPI Robotics Engineering Major~
Aperture Science: 4886A Team Captain 2010-2012
VEX Robotics World Chapmpionship 2012 - Technology Semi-finalists
VEX Robotics World Chapmpionship 2011 - Engineering Division Quarter Finalists

Thu Dec 20, 2012 3:53 pm

Joined: Sun Oct 21, 2012 10:01 pm
Posts: 76
Post Re: Functions with return statments inside of structs
In the original C programming language, you couldn't put functions inside structs (and neither can you in RobotC). It wasn't until C++ that this was possible. However, you can write functions that accept pointers to a struct, and thereby create pseudo-member functions for data structures.

Thu Dec 20, 2012 6:17 pm
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Functions with return statments inside of structs
I think you're looking for "function pointers". Unfortunately, I do not think that these are implemented in ROBOTC yet (At least, I don't think that they are. Someone please correct me if I'm wrong).

sudo rm -rf /

Thu Dec 20, 2012 7:56 pm
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Functions with return statments inside of structs
You are not incorrect; pointers to functions are not implemented. I am not sure whether they will be, either. I haven't heard anything to that effect from the devs.

= Xander

| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Fri Dec 21, 2012 1:56 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests

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

Search for:
Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.