View unanswered posts | View active topics It is currently Fri Nov 21, 2014 8:11 pm






Reply to topic  [ 4 posts ] 
#Define Struct Issue 
Author Message
Rookie

Joined: Thu Apr 10, 2014 8:44 pm
Posts: 4
Post #Define Struct Issue
I'm trying to #define a struct that represents a robot's drivetrain, and am running into an issue when trying to set a value in said struct. The error view in RobotC is basically telling me there is an unexpected ".". The method and struct are posted below. Any idea what I am doing wrong when I #define it?

#pragma config(Motor, port1, sampleMotor, tmotorVex393_HBridge, openLoop)

typedef struct {
tMotor leftMotor;
tMotor rightMotor;
//The lol int here is just for testing purposes
int lol;
} twoMotorDrive;

void setDriveTrainSize(byte size) {
#define driveTrainSize size
#define driveTrain twoMotorDrive
driveTrain.leftMotor = sampleMotor;
}


Thu Apr 10, 2014 9:51 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Tue Oct 09, 2012 10:34 am
Posts: 192
Post Re: #Define Struct Issue
Hi Octogonapus,
From what you've posted so far, I can't understand exactly what you're trying to do. Can you post some more code, or give a broader, but more detailed, description of what you're trying to accomplish?

Thanks,
--Ryan

_________________
Ryan Cahoon
CMU Robotics Academy
RVW Software Developer

Robot Potato Head; Virtual NXT


Fri Apr 11, 2014 5:09 pm
Profile
Moderator
Moderator

Joined: Thu Jan 03, 2013 5:10 pm
Posts: 198
Location: The plateau north of the Ohio River Valley, also known as Cave Country.
Post Re: #Define Struct Issue
I don't think you can use preprocessor statements in functions. If you are trying to create new variables, know that that is impossible in ROBOTC due to the lack of the "new" keyword. If you are trying to assign to existent variables, just use "=".

_________________
I'm not a robot! I'm british! ~ quote from an asparagus
I am not a robot! I am a unicorn! ~ quote from a robot


Mon Apr 14, 2014 4:50 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Tue Oct 09, 2012 10:34 am
Posts: 192
Post Re: #Define Struct Issue
Coder A wrote:
I don't think you can use preprocessor statements in functions. If you are trying to create new variables, know that that is impossible in ROBOTC due to the lack of the "new" keyword. If you are trying to assign to existent variables, just use "=".


You can use preprocessor statements wherever you would like; the C preprocessor has no knowledge of functions. A macro will be in effect from the point in the source file where it is defined until the end of the file or when it is undefined using a #undef statement.

I'm not sure what you mean by ROBOTC can't create new variables - this is very possible. If you mean that ROBOTC has no built-in dynamic memory allocation like C++ or Java, then I believe this is correct.

----

To the OP, since you haven't responded yet, here's an explanation of what your current code does. Like I said, if you can give us a better idea of what you're trying to accomplish, then we can give you better advice.

Octogonapus wrote:
Code:
typedef struct {
   tMotor leftMotor;
   tMotor rightMotor;
   //The lol int here is just for testing purposes
   int lol;
} twoMotorDrive;


Here, you're defining a new structure datatype called twoMotorDrive. This generally looks good.

Octogonapus wrote:
Code:
void setDriveTrainSize(byte size) {
   #define driveTrainSize size
   #define driveTrain twoMotorDrive
   driveTrain.leftMotor = sampleMotor;
}


You start defining a function called setDriveTrainSize. This function takes a single argument, size, of type byte.

You define two preprocessor macros, driveTrainSize, and driveTrain, then have a line where you appear to be trying to set a member of a structure variable, but this will not do what you think:

Preprocessor macros do one thing and one thing only: they replace code with other code when the code is compiled (more accurately, immediately before the code is compiled). When your code is compiled, the preprocessor will evaluate the code and the code in the function will be changed to this:

Octogonapus wrote:
void setDriveTrainSize(byte size) {
twoMotorDrive.leftMotor = sampleMotor;
}


Because the second #define line tells the preprocessor that every time it sees the name driveTrain to replace it with the name twoMotorDrive. The preprocessed code is then sent to the compiler.

The compiler then tries to compile the code, but it runs into a problem: twoMotorDrive is the name of a structure type, not a variable. This is the source of the compiler error you are getting.

My guess is that instead of a #define statement, you want a variable declaration statement. The code of your function would instead be something like this:

Code:
void setDriveTrainSize(byte size) {
   twoMotorDrive driveTrain;
   driveTrain.leftMotor = sampleMotor;
}


This may or may not be what you need to do depending on what you're trying to accomplish, though. It's hard to tell without seeing how driveTrain is used further on in your program. I'm also not sure what the size of a drive train would be.

Cheers,
--Ryan

_________________
Ryan Cahoon
CMU Robotics Academy
RVW Software Developer

Robot Potato Head; Virtual NXT


Mon Apr 14, 2014 5:17 pm
Profile
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.