C++ Beginner · Lesson 19 of 22
Namespaces
Avoiding name clashes, using declarations, and the ROS 2 naming style.
Estimated time · 30–45 min
Learning goals
What you will understand
- Explain namespaces in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
A namespace groups names so that two libraries can each have a function called update without clashing. std is the standard library namespace and rclcpp is ROS 2's. Refer to a name with :: as in robot::Motor.
Never write using namespace std; in a header, because it forces every includer to accept every name. A using declaration for one name inside a function is fine. Anonymous namespaces keep helpers private to one .cpp file.
Guided lesson
Work through these steps
- 1.
Read the namespaces example and identify what each line owns or changes.
- 2.
Create a small source file, compile with -std=c++17 and warnings enabled, and run it.
- 3.
Change one input, predict the result before running, then compare the output.
- 4.
Record one compiler or runtime error and explain the correction in your own words.
Worked example
Read it before you run it
namespace robot {
// Open the robot namespace; everything inside belongs to it.
struct Motor { int id{0}; };
// The full name of this type is robot::Motor.
namespace detail {
// A nested namespace for internal helpers.
inline int next_id() { static int n = 0; return ++n; }
// A helper users should not call directly.
} // namespace detail
// Close the nested namespace.
} // namespace robot
// Close the outer namespace.
robot::Motor m;
// Use the qualified name from outside.
m.id = robot::detail::next_id();
// Call the helper by its full name.
using robot::Motor;
// A using declaration brings in exactly one name.
namespace { int helper() { return 42; } }
// Anonymous namespace: helper is visible only inside this .cpp file.What the example is doing
- 1.Read declarations from right to left: identify each value's type, name, and initial value.
- 2.Follow the data through each expression before considering syntax shortcuts.
- 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.
Watch it run · Values in memory
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Namespaces. Expected result: hardware sim
Practice code
Now type it yourself
Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice
#include <iostream>
// TODO: put a Motor struct with a name() function in namespace robot::hardware ("hardware") and another in robot::sim ("sim")
int main() {
std::cout << robot::hardware::Motor::name() << '\n';
std::cout << robot::sim::Motor::name() << '\n';
return 0;
}Expected output
hardware
simStuck? Show the full solution (try it yourself first)
#include <iostream>
namespace robot::hardware {
struct Motor {
static const char* name() { return "hardware"; }
};
} // namespace robot::hardware
namespace robot::sim {
struct Motor {
static const char* name() { return "sim"; }
};
} // namespace robot::sim
int main() {
std::cout << robot::hardware::Motor::name() << '\n';
std::cout << robot::sim::Motor::name() << '\n';
return 0;
}Expected result
How you know it worked
- After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
- Changing the stated input produces the result you predicted.
Practical exercise
Apply the lesson
Put a Motor class in namespace robot::hardware and a Motor class in robot::sim. Use both in one main.cpp with qualified names, then explain why using namespace std; would be a bad idea in a header.
Before you continue
- The program builds with -std=c++17 and no warnings
- You can explain the key rule without reading the example
- The normal and edge cases both have recorded results
If something goes wrong
Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.
Print the inputs and intermediate values, then check conversions, units, and collection order.