Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
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---------------------------------
#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---------------------------------
 
#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-----------------------------
 
#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---------------------
 
#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::::::::::::::::::::::: ?????????????
Posted 14-Sep-12 15:09pm
Trick7379
Edited 14-Sep-12 17:24pm
v2
Comments
Chuck O'Toole at 14-Sep-12 21:35pm
   
It would help if you included the actual linker error message. I don't see it in your post.
Trick7 at 14-Sep-12 22:35pm
   
>>>>undefined reference to 'MyHashList::MyHashList'<<<<<<<<<<<<
>>>>undefined reference to 'MyHashList::Append'<<<<<<<<<<<<
 
This was the linker error..
Chuck O'Toole at 14-Sep-12 22:57pm
   
almost seems like you forgot to add the .o file to the "make" link line
Richard MacCutchan at 15-Sep-12 6:06am
   
How can you say it's working perfectly when it will not build? You forgot to include the MyHashList program into your project.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Things to remember ....
Generally...
1 )Compiler is always more concerned about declarations
And
2 )Linker is always concerned about Definitions.

 
class/function Declaration is enough for compiler .... This is why your program is compiled without any error.
 
Hence when you get any linker error , the reason could be one of the following
 
1 ) You forgot to add CPP file with corresponding function definition to your project.
2 ) If the function/class is in another DLL, you might have forgot to export the function/class from DLL.
3 ) You might be linking to an older lib file of the DLL.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 347
1 DamithSL 305
2 ridoy 278
3 OriginalGriff 224
4 George Jonsson 140
0 OriginalGriff 7,968
1 DamithSL 6,179
2 Sergey Alexandrovich Kryukov 5,524
3 Maciej Los 5,438
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 11 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100