• 0

C++ Issues: .cpp & .h


Question

So, I've been working on learning C++, writing simple programs for different tasks, etc.. I've been doing ALL of my programming in the main.cpp, and I've ran into a few issues.

 

I did a bit of Google Searching, but became incredibly confused.

I have main.cpp, which doesn't require a header file. I know that for every .cpp I have, I need a .h file. So my question is;

Where the heck does my code go? What goes in the .h?

I have;
main.cpp
rename.h
rename.cpp
quality.h
quality.cpp

(I'm breaking my code up to force myself into doing something different..)


I know I'm a scrub, so please keep the trolling and the insults to a minimum. One cannot learn if one is not taught, nor can one learn the proper way, if not shown the right way.

 

Edit: And my co-worker solved the problem.

 

http://www.learncpp.com/cpp-tutorial/19-header-files/

Edited by BinaryData
Link to comment
https://www.neowin.net/forum/topic/1266462-c-issues-cpp-h/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

C and C++ compile one source (.c or .cpp) file at a time, in isolation from each other. A source file can only access definitions textually contained within it.

So either we write our entire program in a single source file, or we need a way to include the content of other source files without actually causing a recompilation of their content. 

This is the purpose of header files: to tell the compiler about the existence of definitions in other source files without actually including these source files. This is fundamentally why header files should only contain declarations and not definitions, although there are many exceptions, and learning how to structure a program correctly with header files is far from obvious.

Fortunately, the vast majority of programming languages don't force you into this conundrum, but it is something you'll have to go through to program in C++.

  • 0
  On 29/07/2015 at 23:18, Andre S. said:

C and C++ compile one source (.c or .cpp) file at a time, in isolation from each other. A source file can only access definitions textually contained within it.

So either we write our entire program in a single source file, or we need a way to include the content of other source files without actually causing a recompilation of their content. 

This is the purpose of header files: to tell the compiler about the existence of definitions in other source files without actually including these source files. This is fundamentally why header files should only contain declarations and not definitions, although there are many exceptions, and learning how to structure a program correctly with header files is far from obvious.

Fortunately, the vast majority of programming languages don't force you into this conundrum, but it is something you'll have to go through to program in C++.

I find that by breaking things up, fixing bugs can be easier. I mean, would you honestly code a game in 1 file, in the main.cpp, or would you have the core of the game in main.cpp, and have the non-core in others.

This topic is now closed to further replies.