C++ Structure Problems

c++, header

Solution

Doing `#include <...>` searches in the include directories (compiler-specific, usually `/usr/include` and a bunch of other ones for Linux, or the compiler installation directory on Windows) , while `#include "..."` searches the current directory. Make sure you use the right one.

No, you're doing it right.

In C++, there are declarations1 and definitions2. Declarations can go anywhere, and there can be as many declarations of the same name as you want in a single translation-unit, but (non-`inline`, non-template, non-internal-linkage) definitions can only be in at most one `.cpp` file (technically called a "compilation unit" or "translation unit") or else you will get a "multiple definition" error at link-time. Also it is worth noting that a definition also serves as a declaration, but not the other way round.

In C++, you can't use a name (function, struct, variable, whatever) before it's declared, like you can in Java, but you can use it before it's defined in most cases by just writing the declaration above the point of usage.

Header files are just there to let you put declarations (and `inline` function definitions, and template definitions) in all the files that need them without having to copy and paste them over and over again in every .cpp file. You actually can write C++ without using header files at all, but it would be really tedious and there would be tons of code duplication.

1 Examples of declarations that are not definitions:

extern bool bar;
class c;
int foo();
int foo();
int foo(); // Can have many declarations of the same name as long as they match

2 Examples of definitions (that are also declarations):

bool bar;
bool baz = false;
class c { int m; };
int foo() { return 45; }
int foo() { return 45; } // Error: multiple definition

Problem

I am a Java developer who is trying to learn C++. The multi-file structure of C++ is strange to me, being a Java developer, spoiled with classes. I am trying to make a .cpp file that can load other .cpp files similarly to Java classes loading other classes. The way I understand it, is that I have 3 files: main.cpp, filetobeloaded.h, and filetobeloaded.cpp all in the same directory. main.cpp will have a ``` #include <filetobeloaded.h> ``` and then filetobeloaded.h will have ``` #ifndef LOOP_H #define LOOP_H void loop_start(); void loop_run(); void loop_init(); #endif /* LOOP_H */ ``` while filetobeloaded.cpp will have ``` void loop_init(){ //load libraries here } void loop_start(){ //this loop runs as long as the user doesn't request the program to close. //In that case, this function will return and the program will exit. } void loop_run(){ //render stuff here, call subroutines } ``` Obviously, I am doing something wrong because my compiler tells me that the line ``` #include <filetobeloaded.h> ``` is invalid because the file doesn't exist. I have checked and filetobeloaded.h and filetobeloaded.cpp are both in the same directory as main.cpp. I have no idea why it is messing up. Questions: Why am I having errors, and how can I fix them? Is there a better approach to divide my source code to different files than what I am doing? Can you explain C++ multi-file structure in a way that a Java developer would understand? I am trying to make a game in C++ with OGL. I am learning C++ vs Java because of speed, fewer memory leaks (I hope), and Steam integration. I don't have a good book on C++, and I have searched all over the internet... Everyone seems to have a different way of doing this and it is very confusing to me...

Original source

Related problems