Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Where do I put the header and .cpp files that I #include in my main program?

0 votes
asked Jul 7, 2020 by Steve Gold (120 points)
I'm having basic problems with the structure of my C++ code. I want to separate the main program from header and class files. Here is a sample. Where do I put the card.h file and the card.cpp file?

#include "card.h"   // where do I put the card.h and card.cpp files so I can access them in this program?

int main()

{

     card c1 = new card('A','S');   // created the ace of spades

     cout << c1 ;       // prints "Ace of Spades"

     return 0;

}

2 Answers

+2 votes
answered Jul 8, 2020 by Peter Minarik (86,040 points)
In another answer to someone else I created this small project: https://onlinegdb.com/rkpBH1mkD

You can see in Online GDB that the top menu offers a "New File" menu. That's where you can create and add a new file to your current project.

You can access all your projects on the left side panel: "My Projects"

Online GDB will compile all the files for you and build (link) an executable from them and run it for you for testing.

So all you need to do is make sure the declarations (header files) are correctly included, where needed.

You did it correctly: card.h is needed in your main.

Your code should compile.

It's best to share your project and any errors you have encountered to get the right help you need.
0 votes
answered Jul 15, 2020 by Ashish Kumar (230 points)
For the code above, I think you need to include .h file in main.cpp and card.cpp.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...