c++ - 使用模板和继承时出现问题,错误x86_64体系结构的 undefined symbol :

标签 c++ templates inheritance polymorphism hashtable

我在尝试构建项目时遇到此问题,我的函数有6个 undefined symbol 错误:
显示最近的问题
undefined symbol :Hashint::deleteHash(int)
undefined symbol :Hashint::insertHash(int)
undefined symbol :Hashint::searchHash(int)
undefined symbol :Hashint::print()
undefined symbol :HashTable ::HashTable(int)
undefined symbol :Hashint的vtable
我有一个带有内部类(项目)的模板类:

template <class T, class K>
class HashTable
 
{
public:
    enum state {empty, full, deleted};
    class Item
    {
    public:
        T data;
        K key;
        state flag;
        Item(){}
        Item(T d, K k, state f){ data=d; key=k; flag=f;}
        
    };

    
public:
    Item* table;
    int size;
    
    HashTable(int sizeHash);

    virtual ~HashTable() {}
    
    virtual int h1(K k);
    virtual int h2(K k);
    
    //returns the index of the hashing table for the key k at the try number i
     virtual int hash(K k, int i);
     virtual int searchHash(K k); // searches and return the index
    virtual  int insertHash(K k); // insert
    virtual void deleteHash(K k); // delete
     virtual void print();
};

我有从哈希表继承的类Hashint:
class Hashint : public HashTable<int,int>  {
   
public:
    int sizeTable;
    Item* table;
    
    
    Hashint(int size):HashTable<int, int>(size)
    {
        table= new Item [size];
        sizeTable = size;
        for (int i=0; i<sizeTable; i++)
            table[i].flag = empty;
    }
        
    int h1(int k);
    int h2(int k);
    int hash(int k, int i);
    int searchHash(int k);
    int insertHash(int k);
    void deleteHash(int k); 
    void print();    
};
我也有我的Hashtable类的cpp文件,但是如果我发布它,发布将太长...
我不明白这是什么问题,继承做错了吗?还是问题在于模板?
如果您能帮助我,我将非常高兴!
感谢你!!

最佳答案

在实例化模板类之前,必须先定义模板类内部的功能。将函数定义添加到头文件中,它应该可以工作。
检查此代码,例如:

// class templates
#include <iostream>
using namespace std;

template <class T>
class mypair {
T a, b;
public:
mypair (T first, T second)
  {a=first; b=second;}
T getmax ();
};

template <class T>
T mypair<T>::getmax ()
{
T retval;
retval = a>b? a : b;
return retval;
}

int main () {
mypair <int> myobject (100, 75);
cout << myobject.getmax();
return 0;
}
在主代码中使用getMax之前,已在类外部定义了它
Found this code in this link

关于c++ - 使用模板和继承时出现问题,错误x86_64体系结构的 undefined symbol :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64866105/

相关文章:

c++ - 在 C++ 中更新 vector 或列表的多个调用异步

c++ - 为什么我会收到类型不匹配错误 Crypto++ AES

c++ - Qt 和 OpenGL 如何渲染 PVR

c++ - TBB parallel_for 编译错误

c++ - C++ 中的覆盖函数不起作用

c++ - 无法更改 netbeans 的系统变量 "Path"

c++ - 如何比较作为模板类传递的字符串?

c++ - 使用模板化方法进行类型删除

java - 在子类中使用父类构造函数的困难

ruby-on-rails - Ruby——使用父实例变量的子类