# Having trouble with some source code

This topic is 2535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was reading about header files on wikipedia and decided to follow their example of a header file, but I'm having some compiler time errors and I'm not sure whats wrong:

#ifndef ADD_H_GUARD #define ADD_H_GUARD int add(int a, int b); #endif 

- IMPLEMENTATION FILE: triple.cpp
 #include <iostream> #include "add.h" int triple(int x) { return add(x, add(x,x)); } int main() { triple(2); } 

The error says: undefined reference to 'add(int, int)'
This is my first header file so I really didn't know what to look at. Can anyone lend me a hand?

By the way, the header file and the inplementation file are both saved inside the same folder so I don't think the error comes from there, but just to make sure I wrote down the entire location of my header file in the #include, but I still got the same error.

P.S. I noticed on the wiki example that #include "add.h" used double quotes ( " " ) instead of brackets ( < > ) like the standard language header files use, can anyone tell me why? Or is this just a typo?

##### Share on other sites

If using gcc, its basically something like 'g++ triple.cpp add.cpp -o outfile'.

##### Share on other sites
The difference between "" and <> in the include file is implementation defined, however it specifies which locations are searched for the header file. I always use <> for any 'standard' header file while I use "" for my own header files (from my own library or the program itself). In g++ you can add search paths for header files with the -I option. In Visual C++ you can specify extra directories in the property pages for C++.

##### Share on other sites

If using gcc, its basically something like 'g++ triple.cpp add.cpp -o outfile'.

All the information and files involved in this problem are displayed on my first post.

"If using gcc, its basically something like 'g++ triple.cpp add.cpp -o outfile'." I don't understand, please explain.

##### Share on other sites
In your header, you are declaring a function add(int, int). This basically tells the compiler that this function exists somewhere. However, you are not defining the function anywhere. (the compiler has no idea that add(int, int) actually adds two integers).
You must define add somewhere (either in an add.c file, or possibly right where you define triple). If you define add before triple, there is no need for including 'add.h', since at the point that you use add for the first time, the compiler already knows that it exists.

##### Share on other sites
As bbobak says, you are declaring the function int add(int, int), but you are not defining/implementing it.

Try to put this into your .cpp file:

 int add(int x, int y) { return x + y; } 

And I guess it will work. Since you have declared the function in your header file, you can put it anywhere in your .cpp file, except above the #includes or inside other functions.

Edit: I assume you know that this program will just discard the result from the triple(2); call not producing anything you will be able to see?

Edit2: Minor typos

##### Share on other sites
#include "add.h" int add(int x, int y) { return x + y; }
Code excluding the include statement courtesy of aregee. Just create that .cpp file and then link them:
g++ -o something.exe triple.cpp add.h add.cpp

The rest of your code looks like it should be alright.

##### Share on other sites

If you define add before triple, there is no need for including 'add.h', since at the point that you use add for the first time, the compiler already knows that it exists.

While bbobak is right in his statement, I would strongly advice you to declare all functions you are using inside the header file. You don't want to be dependent on the order of your functions in the scope. Sometimes it will even be impossible to place a function without having a declaration first. (Have a look at this example taken from cplusplus.com:

 // declaring functions prototypes #include <iostream> using namespace std; void odd (int a); void even (int a); int main () { int i; do { cout << "Type a number (0 to exit): "; cin >> i; odd (i); } while (i!=0); return 0; } void odd (int a) { if ((a%2)!=0) cout << "Number is odd.\n"; else even (a); } void even (int a) { if ((a%2)==0) cout << "Number is even.\n"; else odd (a); } 

(Where would you place odd() and even() without a declaration here?)

##### Share on other sites

(Where would you place odd() and even() without a declaration here?)

Wouldn't it be more efficient to declare and define the odd() and even() functions before main()?

##### Share on other sites

[quote name='aregee' timestamp='1315707607' post='4860199']
(Where would you place odd() and even() without a declaration here?)

Wouldn't it be more efficient to declare and define the odd() and even() functions before main()?
[/quote]

The answer is that you can't. You have to declare at least even() in that example to make the program compile. (And as you are suggesting you also have to declare odd() too since odd() is below main().)

Without declarations, the compiler will go through the odd() function, see the reference to even() inside there and then complain since it doesn't know how to handle that function works. If you put even() above odd() the situation will reverse. The compiler will now not know anything about odd() inside the even() function and complain.

To avoid this problem altogether, it is a good idea to always declare your functions(). This also helps other people see what functions are available just having a glance at your header file.

1. 1
2. 2
JoeJ
20
3. 3
frob
18
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632204
• Total Posts
3004753

×