简体   繁体   中英

C++ include header files

I'm learning C++ and following this tutorial: http://www.learncpp.com/cpp-tutorial/19-header-files/ They have named the header file that should be included to add, while I named mine 02MultipleFiles_add.cpp. So, when I get to the include part:

02MultipleFiles_add.cpp:

#ifndef ADD_H
#define ADD_H

int add( int x, int y );

#endif

02MultipleFiles.cpp:

#include "02MultipleFiles_add.h"

errors:
cannot open source file "02MultipleFiles_add.h"
identifier "add" is undefined

In the example, why is it called add.h when the file is called add.cpp?
Why can't I include my file?

Thank you.

您的第一个文件需要从02MultipleFiles_add.cpp重命名为02MultipleFiles_add.h

Header files CAN be called anything, but should, typically be called "something.h", not "something.cpp". Files called "something.cpp" are meant to be passed directly to the compiler, and not used for #include . The filename after #include should be the same as the file is called in the filesystem.

I'm pretty sure you've made a typo in the name of the file you are including, and should rename it to "02MultipleFiles_add.h" instead of "02MultipleFiles_add.cpp".

U have given .Cpp extention to your header file. So just change the

02MultipleFiles_add.cpp

to

02MultipleFiles_add.h

and load and compile your project again. I think it will work for sure

You should put the definition of your function in the .cpp file instead of .h file. I think because of this it is giving error as it is trying to include is again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM