Reputation: 113
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...
Upvotes: 3
Views: 119
Reputation: 5010
See also, https://stackoverflow.com/questions/21593/
This question could be considered a duplicate, but is worded differently than the linked answer. Fundamentally, the C standard does not dictate strictly what the difference is, and you must consult the documentation for your compiler for the definitive answer.
Upvotes: 0
Reputation: 562
In addition to the other answers, you will need to have the include
line in the .cpp file as well.
Also, I would personally have the methods in a class instead of the global class. C++ has a feature where you can have methods (and other things) available globally and not in a class.
EDIT: Since the functions aren't in a class and are defined globally, you don't need an include
line.
Upvotes: 1
Reputation: 1002
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
Upvotes: 8
Reputation: 74078
In C++ you have two forms of include
#include <somefile.h>
and
#include "somefile.h"
The first one looks in system directories only, whereas the second one looks in the current directory as well.
Upvotes: 3
Reputation: 2646
Should be ...
#include "filetobeloaded.h"
When you include files from the current directory, you need to place them in quotes, not in the angle brackets that you have.
#include <filetobeloaded.h> //only looks in the systems directory
Upvotes: 3