Functions in C++

If i was to create a header file that would control a motor would i declare it as such:

#ifndef CODE_H
#define CODE_H

#include “WPILib.h”

class Code {
private:
Timer Time;
public:
Code();
void TurnForTime(Jaguar* Jag,float speed, float time);
void Wait(float time);
~Code();
};

#endif


As a quick note, when you post code, if you the CODE tags (click the # button in the post formatting options), you can post code much prettier formatting.


#ifndef CODE_H
#define CODE_H

#include "WPILib.h"

class Code {
private:
    Timer Time;
public:
    Code();
    void TurnForTime(Jaguar* Jag,float speed, float time);
    void Wait(float time);
    ~Code();
};

#endif

The formatting of this code is correct, and if your just giving some example of your formatting your fine.
When you call the function TurnForTime, your syntax would look like this:

codeObject.TurnForTime(&myJagObject,1.0,5.0);

But… if this is the actual header file you are creating (to go along with some cpp file) then I’d like to caution you on a few things:
a) Code is a bad name to give anything, while it may seem nitpicky to say this, it’s very important to be as descriptive as possible with what you call things.
b) The Wait function you have shown is already provided by the WPILib; and using a wait function will keep all other code from executing (so your motors will continue running a particular speed until the blocking ends), which is ok as long as your doing it on purpose :wink:
c) If your doing this because your having trouble adding a regular function outside of the ones provided by the template, then this is a bit overkill (I remember one of your prev post, so I’m just checkin :slight_smile: ). If you’d like a bit of help with structuring whatever it is your trying to do, feel free to PM me with a description of what your doing and your robot.cpp code(or post the code here) and I can try to help.

Also there are a few other ways you could do what your trying to do (or what it looks like your trying to do)

Somewhere above your class you can declare a macro like this:


#include "WPILib.h"
//this will take a jaguar object (not a pointer to it) 
#define TURN_FOR_TIME(jagObj,speed,duration) {jagObj.Set(speed);Wait(duration);}

The way the above works is different from a function call in a few subtle (but important) ways, the way this works is anyplace you call TURN_FOR_TIME(…) it will essentially be like you actually typed all of the code inside the brackets (rather than jumping to a function and executing).

Macros are handy for doing simple operations like this in a way that looks nicer and more organized in your code :smiley:

the following would do the exact same thing given the above #define



void TeleopContinuous(void) 
{
    //The second line is exactly what the compiler sees when you type the first line
    //The two lines are exactly the same, a macro is kinda like an automatic copy and paste
    TURN_FOR_TIME(myJag, 1.0, 2.0);
    {myJag.Set(1.0);Wait(2.0);};
}

Hope this helps (and isn’t too much info :smiley: )