首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

DevC++编译模板类链接异常

2012-02-11 
DevC++编译模板类链接错误我在用DevC++编译模板类时链接时提示错误:[Linkererror]undefinedreferenceto`Ha

DevC++编译模板类链接错误
我在用DevC++编译模板类时链接时提示错误:
    [Linker   error]   undefined   reference   to   `HashTable <int,   int,   bool> ::found(int   const&)   const '  
    [Linker   error]   undefined   reference   to   `HashTable <int,   int,   bool> ::HashTable() '  
下面是几个文件的内容:
--------testHashTable.cpp--------------
#include   <cstdlib>
#include   <string>  
#include   <iostream>
#include   "miniHashTable.H "  

using   namespace   std;

class   dictionary
{
            struct   entry
            {
                          string   name_;
                          entry*   next_;
                          entry();  
            };  
            HashTable <int>   hashedEntries_;
public:
              dictionary():hashedEntries_(){}
            inline   bool   found(const   int&   keyword)   const
            {
                          return   hashedEntries_.found(keyword);  
            }                
};  

int   main(int   argc,   char   *argv[])
{
        dictionary   myDic_;
        myDic_.found(1);
        system( "PAUSE ");
        return   EXIT_SUCCESS;
}

--------------miniHashTable.H------------
template <class   T,   class   Key=int,   class   Hash=bool>  
class   HashTable
{
            mutable   char*   hashTableName_;
public:
              HashTable();
              bool   found(const   Key&   keyword)   const;  
              char*   name();  
};

--------------miniHashTable.cpp---------
#include   "miniHashTable.H "

template <class   T,   class   Key,   class   Hash>
HashTable <T,   Key,   Hash> ::HashTable()  
:hashTableName_( "DefaultHashTable ")
{}
template <class   T,   class   Key,   class   Hash>
bool   HashTable <T,   Key,   Hash> ::found(const   Key&   key)   const
{
          hashTableName_= "newHashTable ";
          return   true;  
}                  
template <class   T,   class   Key,   class   Hash>
char*   HashTable <T,   Key,   Hash> ::name()
{
          return   hashTableName_;  


}  

以上几个文件编译时出现上述问题。
但奇怪的是如果我把所有的内容放到一个文件里就没可以了,这是为什么呢?

[解决办法]
miniHashTable.H miniHashTable.cpp 里的内容还是合并到 miniHashTable.H里吧. 模板的分离模型目前没几个编译器支持的.

并且, 你的语法也不支持分离模型. 那个要加export关键字的.

并了吧!
[解决办法]
模板类不支持cpp和h分离,写在一个文件里吧

热点排行