Functions in header files

As I understand it u should be able to read in a .cpp file using a header file in between? Here's what I did:

1
2
3
4
5
6
7
8
9
10
//main.cpp:
#include <iostream>
#include "add.h"

using namespace std;
int main()
{
    cout << "2+3=" << add(2,3) << endl;
    return 0;
}


1
2
3
4
5
6
//add.cpp
#include "add.h"

int add(int a, int b) {
	return a + b;
}


1
2
3
4
5
//add.h
#ifndef ADD_H_INCLUDED
#define ADD_H_INCLUDED
int add(int a, int b);
#endif // ADD_H_INCLUDED 


I get the following error: main.cpp:7: undefined reference to `add(int, int)'

Now if I change add.h to:

1
2
3
4
5
6
#ifndef ADD_H_INCLUDED
#define ADD_H_INCLUDED
int add(int a, int b);{
	return a + b;
};
#endif // ADD_H_INCLUDED 


it works. Likewise if I in main write #include "add.cpp" .

Has that last way of including any drawbacks or why must I have a header file in the first place?

Cheers,

Peter
Last edited on
Hey dude,

simple mistake, you forgot the semicolon (:

RIGHT
1
2
3
4
5
6
7
8
9
10
//main.cpp:
#include <iostream>
#include "add.h"

using namespace std;
int main()
{
    cout << "2+3=" << add(2,3) << endl;
    return 0;
}

RIGHT
1
2
3
4
5
6
//add.cpp
#include "add.h"

int add(int a, int b) {
	return a + b;
}


FALSE (BUT NOW RIGHT)
1
2
3
4
5
//add.h
#ifndef ADD_H_INCLUDED
#define ADD_H_INCLUDED
int add(int a, int b) ; /* <-- SEMICOLON */
#endif // ADD_H_INCLUDED  


And dude: Never Ever Never include a .cpp file!

bye and have fun (:
Hey!

That wasn't the problem. U replied seconds before I edited my post (I think). The semicolon was always in the code, it simply dissapeared when I removed the function body to post it in the thread. Sorry about the confusion.

I wonder how add.h can know which file the function add is in?

I'm having fun!

/the Dude
Last edited on
Strange, this Code works for me well!
Which Compiler and IDE are you using?

I'm using Code::Blocks. It's weird because the two experiments shows that main.cpp can find the two other files.
Last edited on
Here is the C::B-Project:

http://www.sendspace.com/file/l9e659

have fun dude (:
I wonder how add.h can know which file the function add is in?


It doesn't.

Also, the code in the original post does not work (even if you remove the semicolon on line 3 of add.h).

Based on what you said in the original post, it looks like add.cpp is not being linked (and possibly not even compiled?).

One of the pains with C::B is that it doesn't add new files to the build targets by default. I'm guessing you neglected to do this.

In your project file viewer or whatever it's called (the bar on the left), right click on add.cpp and go to properties. Then go to the Build tab. Make sure "compile this file" and "link this file" are both checked, as well as the "Debug" and "Release" boxes below them. Then rebuild your project.

Also, in the New File wizard, when you get to the page where enter the name for the file, make sure you check the "Add file to active project", "debug" and "release" boxes (which, strangely are unchecked by default! Why they're unchecked I have no idea. It's very annoying).


THAT SAID, MorningStar's code ought to be working just fine.


I would explain why the original code doesn't work, but I have to leave for work early today =(
Thanks that solves it! Out of ignorance I just used the default settings so the "Debug" and "Release" boxes were unchecked.
A common mistake, actually.

I really don't know why they're unchecked by default. It seems crazy to me.
@Disch
could you please explain why the original code doesn`t work?
There's two major steps involved in building a C/++ program: compilation and linking. Compilation is done separately for each compilation unit (a compilation unit is a source file that's passed to the compiler and is independent of every other file. For example, for the command line g++ main.cpp -c -o main.o main.cpp is the compilation unit). For compilation of a CU to succeed, every symbol used in it has to be at least declared, mostly so that the compiler can do type checking. Compiling a CU produces an object file.
Linking takes all the object files generated and links them to a final executable. In order for it to succeed, every symbol that's referenced (in the case of data, "referenced" means "read, written to, or taken the address of". In the case of code, it means "called or taken the address of") has to be defined exactly once. Less than that causes an undefined symbol error; more, causes a duplicate definition error.
Topic archived. No new replies allowed.