C++#

The VEX IQ (1st gen) C++ API Reference explains the classes, objects, constructors, member functions, and variables used in VEXcode C++ projects.

Use this reference when you want to understand how a device or feature is created, check which function signature to use, or compare related member functions in a class.

C++ commands are used in VEXcode IQ C++ projects. C++ pages are organized differently than Blocks pages because many commands belong to a class. A page may first explain the class and how to create an object, then list the member functions that can be called on that object.

How to Read a C++ Class Page#

A C++ class page usually follows this order:

  1. Introduction - Explains what the class represents and when it is useful.

  2. Derived Classes - Lists any classes that are based on this class, if applicable.

  3. Class Constructor or Constructors - Shows the syntax used to create an object from the class.

  4. Class Destructor - Shows the syntax used to destroy the object and release its resources, if applicable.

  5. Parameters - Explains the constructor parameters used when creating the object.

  6. Example - Shows how to create the object in code.

  7. Member Functions - Lists the functions that belong to the class, often grouped by purpose.

  8. Function Entries - Explains each member function, including available functions, parameters, return values, notes, and examples.

Not every page has every section. For example, some pages do not include derived classes, and some functions do not have parameters or return values.

Common C++ API Elements#

Element

What it means

Class

The C++ type for a device or feature, such as bumper or drivetrain.

Object

A named instance created from a class, such as bumper Bumper1 or drivetrain myDrivetrain.

Constructor

The code used to create an object from a class.

Member Function

A command called on an object, such as myDrivetrain.drive(…).

Available Function

One exact function signature that can be used for a member function. Some member functions have more than one available function.

Parameter

A value passed into a constructor or function to control what it does.

Return Value

A value a function gives back, such as bool, int32_t, or double.

Basic C++ Vocabulary#

Term

What it means

Variable

A name that stores a value, such as int speed = 50;.

Type

The kind of data something stores, such as int, double, or bool.

String

Text inside quotes, such as “Hello”.

int

A positive or negative whole number type, such as 90.

double

A number type that can include a decimal, such as 25.5.

bool

A value that is either true or false.

Argument

A value passed into a member function or function call, such as forward in myDrivetrain.drive(forward).

Constructors and Objects#

Before using member functions from many C++ classes, an object must exist. The constructor shows how to create that object.

For example, a Bumper Switch object can be created by giving the constructor the Smart Port where the sensor is connected:

bumper Bumper1 = bumper(PORT1);

After the object is created, its member functions can be called:

Bumper1.pressing();

In VEXcode, device configuration is often generated automatically by the Device Menu. In VS Code, constructors may need to be written manually.

Choosing the Right Available Function#

Some member functions have more than one available function. Each function uses a different set of parameters, so choose the one that matches the values your project needs to provide.

For example, a drivetrain function may have one version that uses the current velocity and another version that lets the project choose a velocity:

void drive(directionType dir);

void drive(directionType dir, double velocity, velocityUnits units);

Both versions call drive, but the second version gives more control because it includes velocity parameters.

Example Function Entry#

drive#

drive moves the robot forward or reverse forever. The robot will continue to move until it is given another action, like turning or stopping.

Available Functions

void drive(directionType dir);

void drive(directionType dir, double velocity, velocityUnits units);

Parameter

Type

Description

dir

directionType

The direction the robot moves: forward or reverse.

velocity

double

The velocity to drive with from 0% to 100% when using velocityUnits::pct, from 0 to 120 rpm when using rpm, or from 0 to 720 degrees per second when using dps. If no velocity is provided, the robot drives at the current drive velocity.

units

velocityUnits

The velocity unit: velocityUnits::pct (percent), rpm (rotations per minute), or dps (degrees per second).

Return Value

This function does not return a value.

Notes

  • drive is non-waiting. The project will run the next line of code right away.

  • The robot will continue to move until stop is called or another drivetrain movement function runs.

  • isDone and isMoving are not used with drive because drive does not have a target distance.

Example

// Drive forward and back.
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.drive(reverse, 25, rpm);
wait(2, seconds);
myDrivetrain.stop();

Waiting and Non-Waiting Behavior#

Some C++ movement functions wait until an action is finished before the project moves to the next line of code. Other functions start an action and allow the next line to run right away.

  • Waiting - The project waits until the function finishes.

  • Non-waiting - The project starts the function and immediately continues to the next line.

Some functions include a parameter such as waitForCompletion to choose this behavior. For those functions, true usually makes the project wait, while false lets the next line run right away.

Commands in Subsections#

C++ commands are organized by device or topic. For example, commands for the Brain, Controller, Drivetrain, Events, Motion, Smart Port devices, Threads, and Logic are grouped into their own subsections.