Linker Error C++ undefined reference

Linker Error C++ undefined reference

Your header file Hash.h declares what class hash should look like, but not its implementation, which is (presumably) in some other source file well call Hash.cpp. By including the header in your main file, the compiler is informed of the description of class Hash when compiling the file, but not how class Hash actually works. When the linker tries to create the entire program, it then complains that the implementation (toHash::insert(int, char)) cannot be found.

The solution is to link all the files together when creating the actual program binary. When using the g++ frontend, you can do this by specifying all the source files together on the command line. For example:

g++ -o main Hash.cpp main.cpp

will create the main program called main.

This error tells you everything:

undefined reference toHash::insert(int, char)

Youre not linking with the implementations of functions defined in Hash.h. Dont you have a Hash.cpp to also compile and link?

Linker Error C++ undefined reference

Your error shows you are not compiling file with the definition of the insert function. Update your command to include the file which contains the definition of that function and it should work.

Leave a Reply

Your email address will not be published.