C++ basics
Read and write the C++ patterns used throughout the robot examples.
An empty VEXcode C++ project. The shared main.cpp provides the device objects. Shared configuration ↗
On this pageVariables and units
Variables and units
A variable holds a value with a type. Use double for measurements that can contain fractions, int for whole-number counters, and bool for true/false conditions. const prevents reassignment after initialization.
Include units in names when they are not obvious: targetInches, timeoutMs, and headingDegrees are easier to check than target, time, and angle. C++ will not stop you from accidentally adding inches to millimeters. For fractional arithmetic, use a decimal operand: 1.0 / 2.0 gives 0.5, while integer division 1 / 2 gives 0.
| Declaration | Meaning |
|---|---|
| double distanceInches = 24.0; | A fractional distance in inches. |
| int settledMs = 0; | A whole-number elapsed-time counter. |
| bool finished = false; | Whether an operation has finished. |
| const double wheelDiameter = 3.25; | A value that is not reassigned. |
References: cppreference: C++ fundamental types (opens in a new tab)
Functions and return values
A function gives a reusable operation a name. Parameters receive input; a return value reports a result. A void function returns no value. A bool movement helper can report whether it reached the target or timed out.
Add inchesToMillimeters() above autonomous(), then replace autonomous() with the example shown. Variables declared inside a function normally belong to that function’s scope. Devices defined at file scope can be accessed by the callbacks in that file.
double inchesToMillimeters(double inches) {
return inches * 25.4;
}
void autonomous() {
const double targetInches = 24.0;
double targetMm = inchesToMillimeters(targetInches);
bool closeEnough = std::fabs(targetMm - 609.6) < 0.1;
if (closeEnough) {
Brain.Screen.print("Conversion OK");
}
}References: cppreference: C++ functions (opens in a new tab)
Conditions and comparisons
Use == to compare values and = to assign a value. Combine conditions with && for “and”, || for “or”, and ! for “not”. An early return can stop a routine when a sensor is not ready.
Do not wait for a measured distance to be exactly equal to a target. Sensor resolution and motion make equality unreliable. Compare the absolute error with a tolerance and, when necessary, require it to stay there.
if (!sensorsReady) { stopDrive(); return; }
if (std::fabs(targetInches - measuredInches) < 0.3) { /* inside tolerance */ }Loops and timing
A while loop repeats while its condition is true. A for loop is useful for a known number of repetitions. Every repeatedly running robot-control loop should allow time for other work using wait().
A wait(10, msec) requests a pause; it is not a guarantee that the entire loop takes exactly 10 ms. Use a timer to measure dt for derivative and integral calculations. Put a timeout on any loop waiting for physical movement or a sensor condition.
References: PROS: Multitasking and synchronization (opens in a new tab)
Read compiler errors
- Start with the first error, not the last. One missing brace can cause many later messages.
- Check the named line and the preceding line for a missing semicolon, parenthesis, or brace.
- “Not declared” usually means a spelling mismatch, wrong scope, or a function used before declaration.
- “Redefinition” often means you pasted a second device object, callback, or main() into the project.
- “No matching function” means the argument types or count do not match the API. Check the VEXcode C++ reference, rather than a PROS example.
Sources for this guide
Examples are written for VEXcode C++. PROS and LemLib references explain related concepts; their APIs are different.
- cppreferenceC++ fundamental types (opens in a new tab)
Reference for boolean, integer, and floating-point types.
- cppreferenceC++ functions (opens in a new tab)
Parameters, return values, declarations, and definitions.
- PROSMultitasking and synchronization (opens in a new tab)
Why loops need delays and why concurrent tasks need an explicit ownership and synchronization model. PROS task APIs are not VEXcode APIs.