0

Beginner makefile guy trying to figure out what is wrong here. On make I'm getting the truly ugly error

" undefined reference to `Class::Method(std::__cxx11::basic_string, std::allocator >)' "

Maybe someone can help me understand what's going on here. When I stick all my code in one big main.cpp I have no issues with compilation or running the code. But when I separate the code into their respective header and cpp files I get the referenced ugly error.

Makefile

CXX = g++
CXXFLAGS = -Wall -g

main: main.o Hash.o 
    $(CXX) $(CXXFLAGS) -o main main.o

main.o: main.cpp Hash.cpp
    $(CXX) $(CXXFLAGS) -c main.cpp

Hash.o: Hash.cpp Hash.h
    $(CXX) $(CXXFLAGS) -c Hash.cpp

Source code

Main.cpp

#include "Hash.h"
#include <iostream>

int main() {

    HashTable Hash;
    std::string inputString = "Pot";
    Hash.Add2Hash(inputString);
    std::string returnVal = Hash.GetHashValue(inputString);
    std::cout << returnVal << std::endl;
}

Hash.h

#include <string>

#define HASH_SIZE 100000

typedef unsigned int uint_t;

struct HashElement_t{
    HashElement_t * Next;
    std::string Value;
    uint_t Key;
};

class HashTable {
    private:
        HashElement_t * HashArr[HASH_SIZE];
    public:
        void Add2Hash(std::string InsertionValue);
        uint_t GenerateHashIndex(char * c); 
        std::string GetHashValue(std::string KeyValue);
}; 

Hash.cpp

#include "Hash.h"
#include <string>

void HashTable::Add2Hash(std::string InsertionValue) {

    uint_t HashIndex = GenerateHashIndex(&InsertionValue[0]);

    HashElement_t * InsertionElement = new HashElement_t;
    HashElement_t * HashPosition =  HashArr[HashIndex];
    InsertionElement->Key = HashIndex;
    InsertionElement->Value = InsertionValue; 
    InsertionElement->Next = NULL;

    if(HashPosition != NULL) {
        while (HashPosition->Next != NULL) {
            HashPosition->Next = InsertionElement;
            HashPosition = HashPosition->Next; 
        }       
    } 

    this->HashArr[HashIndex] = InsertionElement;
}

uint_t HashTable::GenerateHashIndex(char * c) {
    uint_t val{10};
    while (*c != NULL) {
        val *= *c;
        c++;
    }
    return val % HASH_SIZE;
}

std::string HashTable::GetHashValue(std::string KeyValue) {
    uint_t HashIndex = GenerateHashIndex(&KeyValue[0]);
    return this->HashArr[HashIndex]->Value;
}

ERROR MESSAGE

$ make
g++ -Wall -g -c main.cpp
g++ -Wall -g -o main main.o
main.o: In function `main':
C:\Users\Name\Documents\Coding\Leet Coding/main.cpp:9: undefined reference to `HashTable::Add2Hash(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:\Users\Name\Documents\Coding\Leet Coding/main.cpp:10: undefined reference to `HashTable::GetHashValue(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:5: main] Error 1
  • 1
    Look at the command that gets executed that produces the error message. Keep looking at it until you see something that's very obvious, glaringly missing. – Sam Varshavchik May 09 '20 at 03:25
  • @SamVarshavchik To be fair I wasn't very clear what I was looking for, I was hoping to understand why it wasn't defined. I have defined the function, and included it in the main file. The function parameters are identical. I VERIFIED this by putting it all in one file and running make, so I am really lost here. – user3475916 May 09 '20 at 03:59
  • I agree that the function parameters are identical. Now, have you actually looked at the command that produces the error, like I suggested? Do you see anything that's obviously missing there? How about this: retype the same command yourself, maybe that'll help you focus. How about a multiple choice pop quiz: `g++ -Wall -g -o main main.o` -- is that the command that produces your error message? Your multiple choice is either "yes", or "no". You included the error message in your question, after all. Anything obvious that's missing here? Do you know what "linking" is and what it does? – Sam Varshavchik May 09 '20 at 04:05

0 Answers0