HEADER FILES

i created a header file for class, it contains add() function save it as a abc.h.i created abc.cpp file which contains a defination of add function.if i include this abc.h header file in another program,why it is showing add function as a undefined reference..

plz reply soon.......
Last edited on
You are probably using yours IDE bad.

Tell us which IDE you are using. Visual C++? Code::Blocks? DevC++? Notepad?
Are you sure the functions are exactly the same in both files?

If this was in your header:

void add(int x);

And this was in your cpp:

int add(int x) { // function body}

Then you'd get undefined reference when linking...
Last edited on
we are using turbo C++ in windows, LINUX terminal mode
hi Texan 40.

ya, iam sure the both functions are same..i am using linux terminology, turbo c++..
give me the header and the implementation, then I might be able to help you.
Last edited on
try
#include "abc.cpp"
in your header file abc.h
why it is showing add function as a undefined reference..


Undefined reference means that whilst the compiler understands how the function should work (which means it understands the name of the function, the input types and the return type), the linker cannot find the actual compiled code that actually does the work.

In this case, if you #include <abc.h> in your code, the compiler will then find the function prototype, and from that will know the name and parameters. Good so far. Then, everywhere you use the actual function, the compiler effectively leaves a note to the linker, saying "here, you put in the compiled code that actually makes this function call work".

So, then the linker comes along. The compiler has presented it with some compiled code (but NOT the compiled version of abc.cpp, because you have not compiled that, or if you have, you have not told the linker where to find it). The linker then goes looking for the compiled function. It does not find it. It comes back and says "undefined reference". You fix this by compiling abc.cpp and making sure the linker can find it.

Under g++, this would be done simply:

g++ main.cpp abc.cpp to build.

Under Turbo C++, who knows how it's done. The above, though, is a simple explanation of your problem.

Topic archived. No new replies allowed.