模板类遇到链接错误。

mylzc 2009-03-14 09:00:33
以下为DBLList.h文件
#pragma once
template <class Type>
class DBLList/*:public DBListADT<Type>*/
{
public:
DBLList(void);
~DBLList(void);
};
以下为DBLList.cpp文件
#include "DBLList.h"
template <class Type>
DBLList<Type>::DBLList(void)
{

}

template <class Type>
DBLList<Type>::~DBLList(void)
{

}
以下为test.cpp文件
#include "DBLList.h"
int main()
{
DBLList<int> list;
return 0;
}
编译时出现两条错误:
1>test.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall DBLList<int>::~DBLList<int>(void)" (??1?$DBLList@H@@QAE@XZ),该符号在函数 _main 中被引用
1>test.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall DBLList<int>::DBLList<int>(void)" (??0?$DBLList@H@@QAE@XZ),该符号在函数 _main 中被引用

如果把DBLList.h改为
#pragma once
template <class Type>
class DBLList/*:public DBListADT<Type>*/
{
public:
DBLList(void){}
~DBLList(void){}
};
DBLList.cpp清空
则编译正确
百思不得其解,望高手解答。
...全文
122 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2009-03-14
  • 打赏
  • 举报
回复
呵呵,合在一起试试~~~
mylzc 2009-03-14
  • 打赏
  • 举报
回复
两段代码都是在vs2008下编译的
draculamx 2009-03-14
  • 打赏
  • 举报
回复
楼主先说下你用的是啥编译器吧。。。
mylzc 2009-03-14
  • 打赏
  • 举报
回复
我想问下为什么3楼的分开写也能编译成功
正常运行呢
chin_chen 2009-03-14
  • 打赏
  • 举报
回复
这几天怎么这么多同样一个问题的。
。。要分开。
mylzc 2009-03-14
  • 打赏
  • 举报
回复
顶一下
fox000002 2009-03-14
  • 打赏
  • 举报
回复
编译时需要的是类声明

链接时需要的实现

模板的话,还是把实现放到头文件里去吧
mylzc 2009-03-14
  • 打赏
  • 举报
回复
但是之前写过一链表

<<<<<<<<<--LList.h--->>>>>>>>>

#pragma once
#include "List.h"
#include "Link.h"

template <class Elem> class LList:
public List<Elem>
{
public:
LList(void);//构造函数
~LList(void);//析构函数
bool Insert(const Elem& itemval);//添加节点
bool Append(const Elem& itemval);//追加节点
bool Delete(Elem&);//删除节点
bool RemoveAll();//清除所有节点
bool Prev();//current_ptr指针向前移动
bool Next();//current_ptr指针向后移动
void SetPos(int pos);//设置current_ptr的位置
bool GetValue(Elem& elemval);//获取current_ptr的数据项
void Print();//打印链表
private:
Link<Elem>* head_ptr;//头指针
Link<Elem>* current_ptr;//当前指针
Link<Elem>* end_ptr;//尾指针
int leftcnt;//右端长度
int rightcnt;//左端长度
void Init();//初始化

};


<<<<<<<<<<<<<------------LList.cpp------------>>>>>>>>>>>>>>>>>>


#include "LList.h"
#include <iostream>
using namespace std;


template <class Elem>
LList<Elem>::LList(void)
{
Init();
}

template <class Elem>
LList<Elem>::~LList(void)
{

}

template <class Elem>
void LList<Elem>::Init()//初始化
{
head_ptr=current_ptr=end_ptr=new Link<Elem>;
leftcnt=rightcnt=0;
}
template <class Elem>
bool LList<Elem>::Insert(const Elem& itemval)
{
current_ptr->_next=new Link<Elem>(itemval,current_ptr->_next);//插入新节点
if(current_ptr==end_ptr)//如果尾部则尾指针指向新添加的节点
end_ptr=current_ptr->_next;
rightcnt++;
return true;
}
template <class Elem>
bool LList<Elem>::Delete(Elem& it)
{
if(current_ptr->_next==NULL)
return false;
it=current_ptr->_next->_dataElement;//获取被删除节点的值
Link<Elem>* del_ptr=current_ptr->_next;
current_ptr->_next=del_ptr->_next;
delete del_ptr;
if(rightcnt==2)
end_ptr=current_ptr;
return true;
}
template <class Elem>
bool LList<Elem>::Append(const Elem& itemval)
{
end_ptr->_next=new Link<Elem>(itemval,NULL);//插入新节点
end_ptr=end_ptr->_next;
rightcnt++;//右端长度+1
return true;
}
template <class Elem>
bool LList<Elem>::RemoveAll()
{

return true;
}
template <class Elem>
bool LList<Elem>::Prev()
{
if(current_ptr==head_ptr)//若为头指针,无法往前移动,返回false
return false;
Link<Elem>* prefind_ptr=head_ptr;
int prefind_len=0;
while(!(prefind_len+1==leftcnt))
{
prefind_ptr=prefind_ptr->_next;
prefind_len++;
}
leftcnt--;
rightcnt++;
current_ptr=prefind_ptr;
return true;
}
template <class Elem>
bool LList<Elem>::Next()
{
if(current_ptr==end_ptr)//若为尾指针,无法往后移动,返回false
return false;
current_ptr=current_ptr->_next;
rightcnt--;
leftcnt++;
return true;
}
template <class Elem>
void LList<Elem>::SetPos(int pos)
{

}
template <class Elem>
bool LList<Elem>::GetValue(Elem& elemval)
{
elemval=current_ptr->_dataElement;
return true;
}
template <class Elem>
void LList<Elem>::Print()
{
Link<Elem>* print_ptr(head_ptr->_next);
while(print_ptr->_next!=NULL)
{
cout<<print_ptr->_dataElement<<endl;
print_ptr=print_ptr->_next;
}
cout<<print_ptr->_dataElement<<endl;
}


这就没有报错。。。。。
KevinYuen 2009-03-14
  • 打赏
  • 举报
回复
模板类整个放在头文件,静态编联的~
  • 打赏
  • 举报
回复
模板的声明跟定义不要分开,都放在头文件里。。
一般要分开地加export关键字。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧