Linker Error C++ "undefined reference "

330,136

Solution 1

Your header file Hash.h declares "what class hash should look like", but not its implementation, which is (presumably) in some other source file we'll 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".

Solution 2

This error tells you everything:

undefined reference toHash::insert(int, char)

You're not linking with the implementations of functions defined in Hash.h. Don't you have a Hash.cpp to also compile and link?

Share:
330,136

Related videos on Youtube

Fox
Author by

Fox

Updated on July 09, 2022

Comments

  • Fox
    Fox almost 2 years

    Possible Duplicate:
    What is an undefined reference/unresolved external symbol error and how do I fix it?

    Trying to compile my program via g++ -o prog1 main.cpp -std=c++0x

    I get the error:

    /tmp/cc1pZ8OM.o: In function `main':
    main.cpp:(.text+0x148): undefined reference to `Hash::insert(int, char)'
    collect2: error: ld returned 1 exit status
    

    main.cpp

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <functional>
    #include "Hash.h"
    
    using namespace std;
    
    int main(int argc, char *argv[]) {
    //preset prime number 
    int prime = 101;
    hash<char> h1;
    int key;
    Hash HashTable;
    
    // check for Request & string parameters
    if(argc != 3) {
        cout << "Run program with 2 parameters. [Lower Case]" << endl;
        cout << "[1] insert, find, or delete" << endl;
        cout << "[2] string" << endl;
    }
    
    if(strcmp(argv[1], "insert") == 0) {
        //Get Hash for argv[2] aka value
        key = h1(*argv[2]);
    
        //check 1
        cout << "Hash: " << key << endl;
    
        key = key % prime;
    
        //check 2
        cout << "Mod 101 Hash: " << key << endl;
    
        HashTable.insert(key, *argv[2]); //PROBLEM here
    
    }
    
    return 0;
    }
    

    Hash.h file:

    #include <iostream>
    #include <cstring>
    #include "LinkedList.h"
    using namespace std;
    
    class Hash {
    //100 slot array for hash function
    LinkedList *hashFN[100];
    
    public:
    void insert(int key, char value);
    //void deleteItem(int key);
    //char* find(int key);
    
    
    };
    

    Any ideas? Using this to build a hash table with set size.

    Edit: Hash.cpp file

    #include <iostream>
    #include <cstring>
    #include "Hash.h"
    
    using namespace std;
    
    void Hash::insert(int key, char value){
    *hashFN[key]->addFront(value);
    cout << "Success!" << endl;
    
    }
    

    Trying to compile via terminal now with:

    g++ -c Hash.cpp -o Hash.o

    g++ -o prog1 main.cpp Hash.o -std=c++0x

    It goes into an infinite loop somehow.

    • ForEveR
      ForEveR over 11 years
      Where is Hash.cpp file? Where is definition of Hash::insert function?
    • Nolan Robidoux
      Nolan Robidoux over 6 years
      Thinking the problem is you're inserting a char * not a char. Having the same error but as far as I can tell everything looks good function-wise. Onward the search goes....
  • Mo Sani
    Mo Sani over 6 years
    What if I want to compile two files separately and then link them together? The linker cannot find functions within the second file however they have been declared in the main file. It seems that the makefile Fox has wriiten in orange box is OK but when I use it, it says 'undefined reference to ....'