Linux 下C++编译错误,哪位帮我搞下,谢谢了
我想实现一个模板,是一个顺序链表
一些功能还没有完成,但是编译应该不会有问题的
代码如下,
---------Sqlist.h
#ifndef SQLIST_H
#define SQLIST_H
//顺序结构
const int MAXLEN=200;
template <typename type>
class Sqlist
{
public:
Sqlist(const type* a, const int size);
public:
void Clear();
bool isEmpty();
int getLength(){return length;}
type& getItem(int i);
type& prior(const int pos);
type& next(const int pos);
int find(const type x);
Sqlist <type> &
insert(int pos,type x);
Sqlist <type> &
remove(int pos);
void print();
private:
type* data;
int length;
};
#endif
------Sqlist.cc
#include "Sqlist.h "
#include <iostream>
using namespace std;
template <typename type>
Sqlist <type> ::Sqlist(const type* a, const int size)
{
if(a)
{
if(size <=0&&size> MAXLEN)
{
cout < < "Create Error.\n ";
exit(1);
}
else
{
length=size;
data=new type[size];
for(int i=0; i <size; i++ )
data[i]=a[i];
}
}
}
template <typename type>
void Sqlist <type> ::Clear()
{
if(!isEmpty())
delete [] data;
cout < < "Clear Error. ";
}
template <typename type>
bool isEmpty()
{
if(length==0)
return true;
else
return false;
}
template <typename type>
type& Sqlist <type> ::getItem(int i)
{
if(data&&(i <length)&&(i> 0))
return data[i];
}
template <typename type>
int Sqlist <type> ::find(const type x)
{
for(int i=0; i <length; i++)
if(data[i]==x)
return i;
return -1;
}
template <typename type>
type& Sqlist <type> ::prior(const int pos)
{
if(pos=0)
{
cout < < "The first element has no prior elem.\n ";
exit(1);
}
else if(pos> 0&&data&&pos <length)
return data[pos-1];
}
template <typename type>
type& Sqlist <type> ::next(const int pos)
{
if(pos=length-1)
{
cout < < "The first element has no next elem.\n ";
exit(1);
}
else if(pos> =0&&data&&pos <length-1)
return data[pos+1];
}
------main.cc
#include "Sqlist.h "
#include <iostream>
using namespace std;
int main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
Sqlist <int> s(a,10);
if(s.isEmpty())
cout < < "this list is empty.\n ";
else
cout < < "this list is not empty.\n ";
cout < < "length of the list is: " < <s.getLength();
return 0;
}
编译错误信息:
/tmp/cczDM7Rd.o: In function 'main ':
main.cc:(.text+0xe0): undefined reference to 'Sqlist <int> ::Sqlist(int const*, int) '
main.cc:(.text+0xeb):undefined reference to 'Sqlist <int> ::isEmpty() '
collect2: ld returned 1 exit status
[解决办法]
把sqlist.h和sqlist.cc合成一个文件,取名sqlist.h