未定义的参考链接器错误 [英] Undefined reference linker error

查看:75
本文介绍了未定义的参考链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了2个类,以制作单个链接列表MyHastList.h和MyHastList.cpp,其完美执行的代码如下!
---------- MyHashList.h ---------------------------------

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 -------------------------- -------



---------------------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);
    }
}




-------------------------------------------------- --------------------------
但是,当我尝试用它掩盖HashMAP时.代码显示链接器错误:
<>>>对``MyHashList :: MyHashList''的未定义引用 >>>>对``MyHashList :: Append''的未定义引用
等等..但是没有编译错误

以下是我的哈希映射类的代码:::

------------ MyHashTable.h -----------------------------




----------------------------------------------------------------------------
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 ---------------------



---------------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::::::::::::::::::::::: ?????????????

推荐答案

要记住的事情....
通常...
1)编译器始终更关注声明
还有
2)链接器始终关注定义.


类/函数的声明对于编译器来说是足够的....这就是为什么您的程序编译时没有任何错误.

因此,当您收到任何链接器错误时,原因可能是以下之一

1)您忘记将具有相应功能定义的CPP文件添加到项目中.
2)如果函数/类在另一个DLL中,则您可能忘记了从DLL中导出函数/类.
3)您可能正在链接到DLL的较早的lib文件.
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.


这篇关于未定义的参考链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆