Compiling a class with a separate source and header

EDIT: The solution to the problem is this: http://www.jusuchyne.com/codingchyne/2011/03/codeblocks-failed-to-find-the-header-file/

It will not compile, I have the following errors:

  • foo.h there is no such file in the directory;
  • foo has not been declared;
  • num has not been announced in this area
  • foo is not a class or namespace

This is at least strange because I just used the "Create a new class" code blocks and then added it to this project. This is the source code:

Title:

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

cpp

 #include "foo.h" foo::foo() { num = 10; } void foo :: set_num(int set) { num = set; } int foo :: get_num() { return num; } 

Ignore the calz itself and what it does, the problem is that it does not compile, although I used the default code class creation setting.

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 ===| 
+4
source share
3 answers

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

Perhaps this link also helps, since the code block is having problems.

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

+2
source

It should be a comment, but I don't have a 50 reputation yet ...

Can you go to the source directory on the command line and try to compile manually to make sure the error is not related to the IDE?

If your IDE uses g ++ (maybe it is), then the command will be g++ foo.cpp

+2
source
  • Open Windows Explorer
  • Go to the folder containing the files
  • Make sure the title is called "foo.h" (you know Explorer sometimes hides file extensions, right?)

If this is not the case, your compiler does not work.

+1
source

Source: https://habr.com/ru/post/1479023/


All Articles