c++codeblocks

Compiling a class with separate source and header


It won't compile, I have the following errors:

It is odd, to say the least, because I just used the code blocks "Create a new Class" and then added it to this project. This is the source code:

Header:

#ifndef FOO_H
#define FOO_H
class foo
{
    private:
    int num;
    public:
        foo();
    void set_num(int set);
    int get_num();
};
#endif // FOO_H

the cpp

#include "foo.h"

foo::foo()
{
    num = 10;
}

void foo :: set_num(int set)
{
    num = set;
}

int foo :: get_num()
{
    return num;
}

Disregard the calss itself and what it does, the problem is that it doesn't compile even though I used the default code blocks class creation setting.

The errors:

C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|1|error: foo.h: No such file or directory|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|3|error: 'foo' has not been declared|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|3|error: ISO C++ forbids declaration of 'foo' with no type|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp||In function 'int foo()':|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|5|error: 'num' was not declared in this scope|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|6|warning: no return statement in function returning non-void|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|8|error: 'foo' is not a class or namespace|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp||In function 'void set_num(int)':|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|10|error: 'num' was not declared in this scope|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|13|error: 'foo' is not a class or namespace|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp||In function 'int get_num()':|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|15|error: 'num' was not declared in this scope|
||=== Build finished: 8 errors, 1 warnings ===|

Solution

  • If the header is not in the same directory you must either specify the path in the include command, or you must add -I Path directive to your makefile or include settings.

    Maybe this link also helps as codeblock seems to have problems.

    http://www.jusuchyne.com/codingchyne/2011/03/codeblocks-failed-to-find-the-header-file/