3

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:

  1. Why am I having errors, and how can I fix them?

  2. Is there a better approach to divide my source code to different files than what I am doing?

  3. 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...

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mad3ngineer
  • 113
  • 2
  • 11
  • It might be interesting to know what you mean by "loading classes". There is no reflection in C++ so the ordinary run of the mill class loader is very hard to implement. – pmr Feb 08 '14 at 02:05
  • I am not sure what you mean that you are spoiled with classes in Java. The purpose of C++ is to introduce classes to C programmers. The code you have presented is mostly just C, though it is also compatible with a C++. The #include directive just copies the contents of the indicated file into the current file. Usually, the included file contains just declarations. In C++, it may also include inline definitiions. Beware, that since #include is a preprocessor directive, it can (and sometimes does) include anything. – casualcoder Feb 08 '14 at 03:32
  • You might also want to check the following resource: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – casualcoder Feb 08 '14 at 03:44
  • @pmr I mean creating an instance of the class. – Mad3ngineer Feb 09 '14 at 20:05
  • @casualcoder I am learning C++ because it is faster than Java, and has a closer relationship to OpenGL. The reason I said spoiled is because I didn't realise how powerful c++ really is until now. I only saw the C part of C++ and didn't grasp all the OOP part of it. Also I did not notice this comment til today. user3175411's post explained c++ well to me, being a java guy. I did some more googling from where he pointed me, and now I am loving the pointers in C++. I will definately find those useful for my programs. – Mad3ngineer Feb 09 '14 at 20:08
  • @Mad3ngineer You might want to consider that creating objects is usually done by object constructors. You should only resort to factory-like patterns if it cannot be avoided. – pmr Feb 09 '14 at 21:06

5 Answers5

8
  1. 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.

  2. No, you're doing it right.

  3. 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
user3175411
  • 1,002
  • 7
  • 13
  • Thanks for the answer. It solved my problem 100%, and I am back to programming. I think I kind of have an idea of how c++ relates to java now, thanks to you. – Mad3ngineer Feb 08 '14 at 02:21
3

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
Josh Engelsma
  • 2,636
  • 14
  • 17
3

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.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
1

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.

saiarcot895
  • 554
  • 5
  • 16
0

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.

Community
  • 1
  • 1
casualcoder
  • 4,770
  • 6
  • 29
  • 35
  • I did not see that post when I googled. I personally didn't even know what to google... I am appreciative that people are willing to help me out, even though I miss similar questions. – Mad3ngineer Feb 09 '14 at 20:12