• 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.
  • Posts

    • Ashes of the Singularity II announced promising even larger battles, new faction, and more by Pulasthi Ariyasinghe A sequel for the 2016-released Ashes of the Singularity has officially been confirmed, and the original developer and publisher duo, Oxide Games and Stardock Entertainment, are back for this release too. Check out the debut trailer above. Ashes of the Singularity II is under development with a significantly expanded team compared to the original, according to today's announcement. It will be adding in a range of fresh units and gameplay systems for real-time strategy fans to dive into. More options for strategic gameplay, more intricate base building, and better enemy AI are listed as features for the new game as well. "Ten years ago, we set a new, literal benchmark for RTS games with massive battles and groundbreaking technology," said Brad Wardell, CEO of Stardock Entertainment. "With Ashes of the Singularity II, we’re raising the bar even higher, delivering strategic gameplay depth that RTS fans have always dreamed of.” The storyline of Ashes of the Singularity II will take place in 2031, with a complete single-player campaign, as well as cooperative and competitive multiplayer modes, being confirmed as features today. Moreover, a third faction is joining the fray this time, with Humans joining the fight alongside the original games' Post-Human Coalition and the Substrate. "Obviously, in the first game, the number one request was to have a human faction," adds Wardell. “Back then, we just couldn’t support having thousands of organic, walking, squishy people in the world and thus had to design in favor of machines. We’re really excited to bring the humans into the war and watch how they fare against the massive mechanical armies of the Substrate and PHC.” Ashes of the Singularity II is targeting a broad 2026 release window, with the team hoping to launch the title as the original celebrates its 10th anniversary. Its Steam store page is already up for wishlisting over here. Disclaimer: Neowin's relationship to Stardock
    • How exactly is a FREE image generation model a... Ponzi scheme?!?! I don't love all of these Gen-AI things flooding the market but there's nothing "ponzi" about it.
    • I can't decide if this is tacky or a really fun move for more casual players like myself to enjoy. I'll be curious to see how we are to fly to fictional land masses on a real globe.
  • Recent Achievements

    • One Month Later
      SamZrize earned a badge
      One Month Later
    • Week One Done
      SamZrize earned a badge
      Week One Done
    • One Year In
      SamZrize earned a badge
      One Year In
    • One Year In
      barracuda earned a badge
      One Year In
    • One Month Later
      barracuda earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      725
    2. 2
      +FloatingFatMan
      189
    3. 3
      ATLien_0
      180
    4. 4
      Xenon
      113
    5. 5
      neufuse
      109
  • Tell a friend

    Love Neowin? Tell a friend!