Undefined reference linker error

I made 2 class to make a single linked list MyHastList.h and MyHastList.cpp and its working perfectly following is the code!!!!

----------MyHashList.h---------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#ifndef MYHASHLIST_H
#define MYHASHLIST_H

class MyHashListEntry{
  public:
      int key;
      char* value;
      MyHashListEntry* next;
      MyHashListEntry(int keys,char* values);
};

class MyHashList{
  public:
      MyHashList();
      ~MyHashList();
      void Append(int key,char* value);
      void Prepend(int key,char* value);
      char* FindValue(int key);
      void Remove(int key);
      bool IsEmpty();
      void PrintHashList();
  private:
      MyHashListEntry* first;
      MyHashListEntry* last;
};

#endif

---------------------MyhashList.cpp---------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream>
#include<conio.h>
#include "MyHashList.h"
using namespace std;

MyHashListEntry::MyHashListEntry(int keys,char* values){
key=keys;    value=values;    next=NULL;
}

MyHashList::MyHashList(){
first=last=NULL;
}

bool MyHashList::IsEmpty(){
if(first==NULL) return true;   return false;   
}

void MyHashList::Append(int key,char* value){
    MyHashListEntry* element = new  MyHashListEntry(key,value);
    if(IsEmpty()) first=last=element;
    else{
         last->next=element;
         last=element;
    }
}

void MyHashList::Prepend(int key,char* value){
    MyHashListEntry* element = new  MyHashListEntry(key,value);
    if(IsEmpty()) first=last=element;
    else{
         element->next=first;
         first=element;
    }
}

char* MyHashList::FindValue(int key){
    if(IsEmpty())  return NULL;
    MyHashListEntry* element=first;
    if(first==last){
         if(element->key==key) return element->value;
         else return NULL;
    }
    else{
         if(element->key==key) return element->value;
          do{
              element=element->next;
              if(element->key==key) return element->value;
          }while(element->next!=NULL);
    }
    return NULL;
}

void MyHashList::Remove(int key){
    if(IsEmpty())  return;
    MyHashListEntry* element=first;
    if(first==last){
         if(element->key==key) first=last=NULL;
    }
    else{
         if(element){
            if(element->key==key){  
                first=element->next;
                delete element;
                return;
            }
         }
         for(element;element->next!=NULL;element=element->next){
              MyHashListEntry* sub=element->next;                                              
                 if(sub->key==key){ 
                     if(sub==last){
                        last=element;  
                        last->next=NULL;
                        delete sub;
                        return;
                     }else{                
                         element->next=sub->next;
                         delete sub;
                         return;
                     }
                 }
         }
    }
}

void MyHashList::PrintHashList(){
    if(IsEmpty())      cout<<"NULL\n";
    MyHashListEntry* element=first;
    if(first==last){
         cout<<"Key = "<<element->key<<" value = "<<element->value<<" \n";
    }else{
          cout<<"Key = "<<element->key<<" value = "<<element->value<<" \n";
          do{
              element=element->next;
              cout<<"Key = "<<element->key<<" value = "<<element->value<<" \n";
          }while(element->next!=NULL);
    }
}

----------------------------------------------------------------------

But When I tried to mask HashMAP with it. Code showed Linker error :
>>>>undefined reference to 'MyHashList::MyHashList'<<<<<<<<<<<<
>>>>undefined reference to 'MyHashList::Append'<<<<<<<<<<<<

so on.. but there was no compilation error

Following is the code for my Hash map class:::


------------MyHashTable.h-----------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef MYHASHTABLE_H
#define MYHASHTABLE_H
#include "MyHashList.h"

class HashEntry{
   public:
       MyHashList* myList;
       HashEntry();
};


class HashTable{
   public:
       HashTable(int size); 
       void Put(int key,char* value);
       char* Get(int key);
   private:
      HashEntry** table;
      int SIZE;  
};

#endif


---------------MyHashTable.cpp---------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include "MyHashTable.h"
using namespace std;

HashEntry::HashEntry(){
    myList= new MyHashList();
}

HashTable::HashTable(int size){
   SIZE=size;
   table = new HashEntry*[SIZE];
   for (int i = 0; i < SIZE; i++)      table[i]=new HashEntry();
}

void HashTable::Put(int key,char* value){
   table[key%SIZE]->myList->Append(key,value);
}

char* HashTable::Get(int key){
return ( table[key%SIZE]->myList->FindValue(key) );
}

Please help::::::::::::::::::::::: ?????????????
Last edited on
Anyone Please Help?
You need to cast MyHashList's object into MyHashTable's object.. i think that may be solved your problem..
Please copy paste the linker errors exactly.

Also, there is no need to include haslist in your table header, foward declare, #include in tables cpp(this doesn't fix ur problem, just cleans your header)
Last edited on
What tool are you using to build your app?

As I can see the MyHashList constructor and Append method, I am wondering if your build config is missing MyHaskList.cpp -- that you are just building MyHashTable.cpp.

By the "so on.." you mean that there are other methods that are unresolved, yes?

Andy
Last edited on
Following are the Errors:

C:\Users\Shallin\AppData\Local\Temp/cc2Fcaaa.o(.text+0x15f):MyHashTable.cpp: undefined reference to `MyHashList::MyHashList()'
C:\Users\Shallin\AppData\Local\Temp/cc2Fcaaa.o(.text+0x211):MyHashTable.cpp: undefined reference to `MyHashList::MyHashList()'
C:\Users\Shallin\AppData\Local\Temp/cc2Fcaaa.o(.text+0x4c0):MyHashTable.cpp: undefined reference to `MyHashList::Append(int, char*)'
C:\Users\Shallin\AppData\Local\Temp/cc2Fcaaa.o(.text+0x503):MyHashTable.cpp: undefined reference to `MyHashList::FindValue(int)'
collect2: ld returned 1 exit status

Execution terminated


I second andywestken's suggestion, check that.

Also, just for testing, comment out all references to your MyHashList in the "Table" code. Create a local MyHashList inside any method of your table. Do a very simple instance of one, using the constructor and append. If this works without the link error, then I suspect you may have a problem with the way you have your table setup and the access to the actual MyHashList. If you get the same Link error, or a different link error repost.

Also, you said
MyHastList.cpp and its working perfectly following is the code!!!!
. Did you test this from main? Or some other translation unit?
Topic archived. No new replies allowed.