We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Orientation and the Gate step 2 of 3
Read the diagnostic before you touch the code
This starter does not compile. That is deliberate, and it is true of only two problems in this course — this one and the next, both in this track. From Track 1 onward every starter builds and runs, and the lesson is in what it does rather than in whether it does anything.
C++ error messages have a reputation, most of it earned by templates. But the ordinary ones — the ones you will read every day — have a fixed shape, and once you can parse that shape the reputation stops applying to ninety percent of what you actually see.
The shape
unit.cpp:4:23: error: no member named 'lenght' in 'std::vector<int>'
4 | for (int i = 0; i < xs.lenght(); ++i) {
| ~~ ^
Five parts, always in this order:
| Part | Here | What it is |
|---|---|---|
| file |
unit.cpp |
The compile unit. Always this — see problem 3. |
| line:col |
4:23 |
Where the compiler gave up. Not always where you went wrong. |
| severity |
error |
error stops the build; warning does not; note explains a previous line. |
| message |
no member named… |
What it could not do. |
| caret |
~~ ^ |
^ marks the exact token; ~~ underlines the relevant range. |
Read them right to left. The caret tells you where, the message tells you what, and the line number is only useful once you have both.
Line numbers are a hypothesis
The reported line is where the compiler noticed, which is not always where
the mistake is. A missing ; at the end of a class shows up as an error on
the next declaration. A missing > in a template shows up several lines
later, in a construct you did not write.
So the habit worth building on day one: read the message, look at the caret, then decide whether the line is really the problem. Changing the line the compiler named, without doing that, is how people end up “fixing” errors by breaking something else.
note: is not optional reading
A note: is a continuation of the error above it. It has no severity of its
own and it is where the actual explanation usually lives:
error: no matching function for call to 'f'
note: candidate function not viable: no known conversion from 'int' to 'const std::string&'
The error says it failed. The note says why, and the why is the fix.
Your task
int sum_positive(const std::vector<int>& xs)
Return the sum of the elements greater than zero. There are three separate mistakes in the starter, and the compiler will not show you all three at once — it reports the first, and some later ones only become visible after the earlier ones are fixed. That cascade is normal and worth seeing once deliberately.
Fix them one at a time. Read each message before changing anything.
Stuck?
C++ reference solution
Sign in to attempt this problem and reveal the reference solution.