We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Orientation and the Gate step 2 of 3
Read a compiler diagnostic
The starter does not compile because it calls a member function that does not exist. Use the diagnostic to locate that mistake, then review the loop and accumulator before submitting. A successful build alone does not establish that the function returns the right answer.
Read the message and its location
Clang reports the misspelled call with a message like this:
unit.cpp:8:28: error: no member named 'lenght' in 'std::vector<int>'
8 | for (int i = 0; i < xs.lenght(); ++i) {
| ~~ ^
The exact location and formatting depend on the source file and compiler. The useful pieces are:
| Part | Meaning |
|---|---|
unit.cpp |
The file being compiled. The runtime combines your code with a supplied harness. |
8:28 |
The line and column associated with the diagnostic. |
error |
A problem that prevents successful compilation. |
no member named 'lenght' |
The operation the compiler could not resolve. |
| Caret and underline | The relevant location and source range. |
Read the message, inspect the highlighted code, and check what you intended
that code to do. Here, the compiler looked for lenght on a vector. The
function for the number of elements is size().
Check nearby code and notes
A diagnostic can point to where the compiler noticed a problem rather than where it began. For example, a missing delimiter can affect the interpretation of later code. Inspect nearby declarations if the highlighted expression appears correct.
A note: supplies context for another diagnostic. It might identify a
declaration, show a candidate overload, or explain why a conversion failed.
Read related notes together with the main message.
Warnings flag potential issues without normally preventing compilation;
compiler options can promote them to errors. Which warnings appear depends
on the compiler and its options. The separate clang-tidy gate also checks
submitted code. Do not rely on either tool to report every defect.
Your task
int sum_positive(const std::vector<int>& xs)
Return the sum of all elements strictly greater than zero. Ignore zero and
negative values. Return 0 for empty input or when there are no positive
elements. Inputs are chosen so that the sum fits in an int.
For [1, -2, 3, -4, 5], the result is 1 + 3 + 5 = 9.
Address these three issues in the starter:
- Correct the nonexistent member function call.
- Use an index type compatible with the vector's unsigned size, or use a range-based loop to avoid indexing.
- Initialize the accumulator before reading or adding to it.
Fix the build error, then inspect the remaining code and feedback. You may see several diagnostics together, or no warning for a remaining problem. In particular, an uninitialized accumulator is still a bug even if the build succeeds or a test happens to return the expected value.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
C++ reference solution
Sign in to track your attempts toward unlocking the reference solution.