模板类 出现undefined reference to错误

yadain yadain 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

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

望高手指教
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
yadain
C++ 语言 65,214 成员 250,514 内容