However, if you tell the students to #include both the .h and any .cpp files in the main program, then you don't need to separately compile the .cpp files. It's unorthodox, and rather defeats the point of having .h files but it works OK. It sounds like that's what you must be doing.
Re: C++ program question with user defined header files
However, if you tell the students to #include both the .h and any .cpp files in the main program, then you don't need to separately compile the .cpp files. It's unorthodox, and rather defeats the point of having .h files but it works OK. It sounds like that's what you must be doing.
Re: C++ program question with user defined header files
The problem is that when I do so, I (as student) received lots of redefinition errors (for all functions in the student answer box). On the other hand, if I explicitly write the name of the cpp file in compilerargs, there is no problem. However, I do not know how the students will name the extra files.
Re: C++ program question with user defined header files
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.