0

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.

2
  • 1
    You either have a typo in the name "02MultipleFiles_add.cpp", or your problem is that the file should be called "02MultipleFiles_add.h" according to the "main file". Commented Jan 18, 2014 at 10:04
  • just to add #pragma once can replace the #ifndef guard in most circumstances. It's non-standard, but very well supported pre-processor directive. Commented Jan 18, 2014 at 10:15

4 Answers 4

1

Your first file needs to be renamed from 02MultipleFiles_add.cpp to 02MultipleFiles_add.h

Sign up to request clarification or add additional context in comments.

5 Comments

This is the error, does #ifndef ADD_H make it supposed to #include "add.h"? gyazo.com/a1212ac0dd0314df6d30cab0760d2a5f.png
@user3209520 You're not following the tutorial correctly. Your add.h file should not contain the definition of add because the definition is already in add.cpp. Look at what the tutorial says the content of add.h should be. There is nothing after the #endif. The error is telling you that you have multiple definitions of add.
I suspect your IDE is trying to compile the .h file as a side-effect of you adding it to the project/solution originally as a .cpp file. Just remove the .h file from the solution, but leave it remaining on disk such that the preprocessor will still read it when it compiles the .cpp file.
@selbie It's not clear from the screenshots, but they still have add.cpp. So after inclusion, there's one definition in add.cpp and one in main.cpp.
Thank you, I had not read it right. Now I have the definition in 02MultipleFiles_add.h, the function in 02MultipleFiles_add.cpp, and #include "02MultipleFiles_add.h" works, but, what is this <br/><br/><br/>#ifndef ADD_H #define ADD_H<br/><br/> used for? If it says #define ADD_H but I don't use that ADD_H anywhere
1

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

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.