C++ basics

Read and write the C++ patterns used throughout the robot examples.

Before you start

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.

DeclarationMeaning
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.

main.cpp · function and arithmetic examples
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

  1. Start with the first error, not the last. One missing brace can cause many later messages.
  2. Check the named line and the preceding line for a missing semicolon, parenthesis, or brace.
  3. “Not declared” usually means a spelling mismatch, wrong scope, or a function used before declaration.
  4. “Redefinition” often means you pasted a second device object, callback, or main() into the project.
  5. “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.

  1. cppreferenceC++ fundamental types (opens in a new tab)

    Reference for boolean, integer, and floating-point types.

  2. cppreferenceC++ functions (opens in a new tab)

    Parameters, return values, declarations, and definitions.

  3. 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.

Search documentation

↑ ↓ to move Enter to openEsc to close