C++ program question with user defined header files

Re: C++ program question with user defined header files

de către Richard Lobb-
Număr de răspunsuri: 2
The normal pattern for C and C++ projects is that the main program #includes the .h files but not the .cpp files. The latter then need to be compiled separately and all then get linked to an executable by 'ld' (usually invoked by the compiler itself). A single command like "g++ *.cpp -o prog" will do all that for you, but that's not the command that CodeRunner uses: it just compiles the one explicitly-named program. So you would need to modify CodeRunners compile-and-link command.

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.
Ca răspuns la Richard Lobb

Re: C++ program question with user defined header files

de către Albert Levi-
I have a similar issue. In our course, I have to allow the students to upload any cpp file (in addition to their answer pasted to the answer box). To do so I added "*.cpp" to compilerargs.

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.
Ca răspuns la Albert Levi

Re: C++ program question with user defined header files

de către Richard Lobb-

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:

  1. 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.
  2. 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.