Monday, May 24, 2010

C++ linker errors while writing a header file?

Im learning C++, and trying to write a header file called navtools.h which contains the declarations of some useful functions I have written. whenever i compile my "main.cpp", i get lots of errors like





" [Linker error] undefined reference to `CheckValidLat(float)' "





I tried copying another header file from functionx.com (just to see if it worked) and it also compiles with the same errors! can someone help me out?

C++ linker errors while writing a header file?
Here is what you need to do:





main.cpp:





...


#include "navtools.h"


...


CheckValidLat(5.4);


...





In the same folder as main.cpp, you put navtools.h. If you are compiling with MS Visual C++, make sure you add navtools.h to your project.





Remember that #include (and in general any #-directive) literally copies and pastes the referenced file into your .cpp file. So if you don't have the right #include, then it is as if you are using a function that you didn't declare anywhere.
Reply:Hmm i do programm but not in c++ (YET lol) but here are some tips, is the the heder file in the same folder as the rest of the c++ files??? Might it be a syntax erorr??? In the the heder file are all the filenames right??? Well just keep tryin :).
Reply:Seems to me I had similar errors if I included the header file in the build without including the c++ file that had the actual implementation of the method. Try including the obj file from your compiled useful functions as input to the linker.
Reply:Your main.cpp is calling a function CheckValidLat() that takes a float as an argument. The file containing CheckValidLat() needs to be compiled and linked with the main.cpp.





If CheckValidLat() function is already defined in main.cpp then check the data type of the argument being passed to it.


No comments:

Post a Comment