模板类 出现undefined reference to错误

yadain 2010-04-19 12:01:31

linearlist.cpp

#include "linearlist.h"
template <class T>
LinearList<T>::LinearList(int nMaxSize)
{
nMaxListSize = nMaxSize;
length = 0;
element = new T[nMaxSize];
}
template <class T>
LinearList<T>::~LinearList()
{
delete []element;
}
template <class T>
bool LinearList<T>::IsEmpty()const
{
return length==0;
}
template <class T>
int LinearList<T>::GetLength()const
{
return length;
}
template <class T>
bool LinearList<T>::Find(int k,T& x)const
{
if(k<1||k>length)
return false;
x = element[k-1];
return true;
}
template <class T>
int LinearList<T>::Search(const T& x)const
{
for(int i=0;i<length;i++)
{
if(element[i]==x)
return ++i;
}
return 0;
}
template <class T>
bool LinearList<T>::Delete(int k,T &x)
{
if(Find(k,x))
{
for(int i=k;i<length;i++)
{
element[i-1]=element[i];
}
length--;
return true;
}
return false;
}
template <class T>
bool LinearList<T>::Insert(int k,const T &x)
{
if(k<0||k>length)
return false;
if(length == nMaxListSize)
return false;
for(int i=length-1;i>=k;i--)
{
element[i+1] = element[i];
}
element[k] = x;
length++;
return true;
}
template <class T>
void LinearList<T>::OutPut(ostream &out) const
{
for(int i=0;i<length;i++)
cout<<element[i]<<" ";
}
template <class T>
ostream &operator<<(ostream &out,const LinearList<T> &x)
{
x.OutPut(out);
return out;
}

linearlist.h

#ifndef LINEARLIST_H
#define LINEARLIST_H
#include <iostream>
using namespace std;

template <class T> class LinearList;
template <class T>
ostream &operator<<(ostream &out,const LinearList<T> &x);

template <class T>
class LinearList
{
public:
LinearList(int nMaxSize=10);
~LinearList();
public:
bool IsEmpty()const;
int GetLength()const;
bool Find(int k,T& x)const;
int Search(const T& x)const;
bool Delete(int k,T& x);
bool Insert(int k,const T& x);
void OutPut(ostream &out)const;
private:
int length;
int nMaxListSize;
T *element;
};
#endif

main.cpp

#include "linearlist.h"
int main(void)
{
LinearList<int> list(5);
list.Insert(0,1);
list.Insert(1,2);
cout<<endl;
cout<<list<<endl;
return 0;
}

makefile

TARGET=list
OBJECT=main.o linearlist.o
all:$(TARGET)
$(TARGET):$(OBJECT)
g++ -o $(TARGET) $(OBJECT)
linearlist.o:linearlist.cpp linearlist.h
g++ -c linearlist.cpp
main.o:main.cpp linearlist.h
g++ -c main.cpp
.PHONY:clean
clean:
rm -f $(TARGET) $(OBJECT)

错误
main.cpp:(.text+0x1b): undefined reference to `LinearList<int>::LinearList(int)'
main.cpp:(.text+0x3f): undefined reference to `LinearList<int>::Insert(int, int const&)'
main.cpp:(.text+0x63): undefined reference to `LinearList<int>::Insert(int, int const&)'
main.cpp:(.text+0x8b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <int>(std::basic_ostream<char, std::char_traits<char> >&, LinearList<int> const&)'
main.cpp:(.text+0xac): undefined reference to `LinearList<int>::~LinearList()'
main.cpp:(.text+0xc7): undefined reference to `LinearList<int>::~LinearList()'
collect2: ld returned 1 exit status

望高手指教
...全文
1588 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaaracom 2011-10-27
  • 打赏
  • 举报
回复
不能分离编译亮了!刚才还为此苦恼!
tyzqqq 2010-09-12
  • 打赏
  • 举报
回复
YT158828 2010-04-19
  • 打赏
  • 举报
回复
template <class T>
bool LinearList<T>::Find(int k,T& x)const
{
if(k<1||k>length)
return false;
x = element[k-1];
return true;
}

const 函数,不可以改变参数值..这个似乎有问题
昵称很不好取 2010-04-19
  • 打赏
  • 举报
回复
模板类不支持分离实现,你把声明和实现文件放一起吧
或是使用export关键字,但需要编译器支持这个关键字
或者在用到这个类的时候把.cpp文件也include进来
老吴笔记 2010-04-19
  • 打赏
  • 举报
回复
#include "linearlist.h"
这是哪个?这样没法看
pengzhixi 2010-04-19
  • 打赏
  • 举报
回复
请不要分离编译。
aweer 2010-04-19
  • 打赏
  • 举报
回复
没有哪几个编译器可以用分离编译,还是用包含编译吧


.h:
#ifndef XXX_H
#define XXX_H
....
....
#include"cpp"
#endif

cpp:
#ifndef XXX_CPP
#define XXX_CPP
#include".h"
..
...
#endif

main:
#include"h"
..
...

65,187

社区成员

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

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