We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Orientation and the Gate step 3 of 3
What gets compiled with your code
You never write main here. Something else does, and knowing what it is
turns two confusing failures into obvious ones.
One file, three parts
Your submission is concatenated with two other pieces into a single translation unit and compiled once:
┌──────────────────────────────┐
│ 1. your code │ ← line 1 of the unit is line 1 of yours
├──────────────────────────────┤
│ 2. a small JSON helper │ ← injected, namespace `json`
├──────────────────────────────┤
│ 3. the problem's harness │ ← provides int main()
└──────────────────────────────┘
Your code is first, and that is the whole reason line numbers work. Line 12 of the diagnostic is line 12 in your editor. If the order were reversed you would be reading errors offset by however long the injected part happened to be that week.
Two consequences follow, and they are the two things that surprise people.
Consequence 1: your includes are yours
Your code is compiled before the helper’s #include lines are seen. So if
you use std::vector, you include <vector> — the fact that the harness
below you includes it is no help, because “below” means “later” to a
compiler reading top to bottom.
This is ordinary C++, not a quirk of this site. Every translation unit includes what it uses. It is worth stating only because the harness is invisible and it is natural to assume it has already set things up.
Consequence 2: errors in code you cannot see are hidden
Diagnostics are filtered to your line range. Anything the compiler blames on the helper or the harness is discarded before you see it, because being shown a warning about a file you cannot open is worse than being shown nothing.
There is one case where that filtering bites, and it is worth recognising:
If your function’s signature does not match what the harness calls, every error lands in the harness, and every error is filtered away.
You get “compilation failed” with no diagnostics and a written explanation instead. That message always means the same thing: the name, the parameter types, or the return type is not what the problem asked for.
Your task
std::vector<std::string> repeat_each(const std::vector<std::string>& words,
int times);
Return a vector containing each input string repeated times times, in
order. repeat_each({"a","b"}, 2) is {"a","a","b","b"}. A times of zero
or less produces an empty vector.
The starter is missing an include. Compile it and read what the compiler says it cannot find; the fix is one line, and the point is that you can now predict why it was missing.
Stuck?
C++ reference solution
Sign in to attempt this problem and reveal the reference solution.