So I'm really confused about headers, implementations, and #include

I still don't get all that, mentioned in the thread title.

Right now I'm using the Code::Blocks IDE, and it's getting me really confused about headers, implementations, and #include.

When I add a class in CodeBlocks, it puts the header file in a folder called include, and puts the cpp file in a folder called src. My Main.cpp file is in the same directory as these folders.

Now without changing anything, I tried to include the header file. Auto complete tells me something like this:

 
#include "include/Foo.h" 


When I compile, however, a lot of errors are thrown:

D:\Dev\C++\Test1\Test\src\Foo.cpp|1|error: Foo.h: No such file or directory|
D:\Dev\C++\Test1\Test\src\Foo.cpp|3|error: 'Foo' has not been declared|
D:\Dev\C++\Test1\Test\src\Foo.cpp|3|error: ISO C++ forbids declaration of 'Foo' with no type|
D:\Dev\C++\Test1\Test\src\Foo.cpp||In function 'int Foo()':|
D:\Dev\C++\Test1\Test\src\Foo.cpp|6|warning: no return statement in function returning non-void|
D:\Dev\C++\Test1\Test\src\Foo.cpp|8|error: expected constructor, destructor, or type conversion before '::' token|
||=== Build finished: 4 errors, 1 warnings ===|

Code Blocks generated the two files to be like this, and I have not modified them:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//Foo.cpp
#include "Foo.h"

Foo::Foo()
{
    //ctor
}

Foo::~Foo()
{
    //dtor
}

//Foo.h
#ifndef FOO_H
#define FOO_H


class Foo
{
    public:
        Foo();
        virtual ~Foo();
    protected:
    private:
};

#endif // FOO_H 


I was hoping someone on this forum could explain to me what is the common method of making classes and folder structures.
You need to set the search path for your project. EDIT: where 'Foo.h' resides

Open the menu: Project->Build Options...->(dialog 'Project build options'->)Search directories

In the dialog 'Project build options' you find also 'Linker settings' where you provide the libraries if any
Last edited on
Put class definitions in header files.
Put inline function definitions in header files.
Put non-inline function declarations in header files.
Put templates in header files.

Put non-inline function definitions in source files.
Put global variables in source files.

Some people use a Javaesque approach and write all the code for a class in a single header file. If you do this all your functions are automatically marked as inline.
Topic archived. No new replies allowed.