模板类 出现undefined reference to错误
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
望高手指教
...全文
相关推荐
C++错误代码详细解释
c++开发时候经常在编译的时候遇到错误,英文不是很好的童靴可以参照一下
【转】undefined reference to错误的解决方法.doc
【转】undefined reference to错误的解决方法.doc
undefined reference to `SetPduPowerConsumptionCnt'错误的解决方法
编译时出现undefined reference to `SetPduPowerConsumptionCnt'错误要如何解决呢?有没有什么好的解决方法?下面小编就为大家解答吧,如果你也遇到了这种情况,可以过来参考下
undefined reference to 'pthread_create'的解决方法
undefined reference to 'pthread_create'的解决方法,需要的朋友可以参考一下
undefined reference to ‘pthread_create’的解决方法
照着GUN/Linux编程指南中的一个例子输入编译,结果出现如下错误: undefined reference to ‘pthread_create’undefined reference to ‘pthread_join’ 问题原因: pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。 问题解决: 在编译中要加 -lpthread参数 gcc thread.c -o thread -lpthr


