I'm afraid this doesn't have an easy nice solution. To manage this properly you need to set up your own question type with a Python-scripted template that manages the whole compile-and-run sequence. The process is explained here, for C rather than C++ but it should be fairly obvious how to adapt it. Although it seems a bit complicated, it doesn't take all that long to set up, and once you've done so you have lots of extra flexibility, such as the ability to perform checks on the student's code or even to edit it (e.g. replacing calls to some function with calls to another that you have instrumented). However, you do need to be careful with defining your own question types as there are some significant pitfalls. Please do read this section of the documentation carefully.
If you wish to avoid this complexity, I can only suggest a couple of in-elegant workarounds:
- Still with the added *.cpp in the compileargs, ask students to submit their answer box code as a separate file, too, with the answer box just containing some harmless non-empty content, such as a comment explaining something possibly relevant to the task.
- Without the added *.cpp in the compileargs, ask students to submit all their code in separate files, including the answer box code. Then, in the answer box, tell them to put a series of #include "myfilename.cpp" statements, one for each of their files.