gcc编译C++文件出错

guanzhouxuezi 2011-06-03 09:35:02
这是我的代码

//SeqList.h
#ifndef SSEQLIST_H_INCLUDED

#define SSEQLIST_H_INCLUDED

#include<iostream>

#define DEFAULT_SIZE 10

template <class T>
class SeqList
{
public:
//构造和析构函数
SeqList();
SeqList(int size);
virtual ~SeqList();
public:
//各种操作函数
int GetLength()const; //获取长度
int LocateItem(T x)const; //定位元素x,返回序号加1
void InsertItem(T x,int i); //在i处插入元素x,若i不符合条件则插入失败
void InsertTail(T x); //在顺序表尾部插入x
void DeleteItem(T x); //删除x元素,若找不到,则删除失败
void DeleteTail(); //删除尾部元素
T &GetItem(int i); //获取位置i处的元素
void SetItem(T x,int i); //修改位置i处的元素
public:
int IsIn(T x); //判断x是否在顺序表中
int IsEmpty(); //空返回1,否则返回0
int IsFull(); //满返回1,否则返回0
public:
void Show()
{
cout<<"顺序表中一共有"<<last+1<<"个元素"<<endl;
for(int i=0;i<=last;i++)
cout<<data[i]<<" ";
cout<<endl;
}
public:
T *data;
int MAXSIZE;
int last;
}


#endif // SSEQLIST_H_INCLUDED


//SeqList.cpp
#include "SSeqList.h"
/*动态分配存储空间*/
template<class T>
SeqList<T>::SeqList()
{
MAXSIZE = DEFAULT_SIZE;
last = -1;
data = new T[MAXSIZE];
}
template<class T>
SeqList<T>::SeqList(int size)
{
if(size<=0)
{
MAXSIZE = 0;
last = -1;
data = NULL;
}
else
{
MAXSIZE = size;
last = -1;
data = new T[MAXSIZE];
}
}
/*插入操作*/
template<class T>
void SeqList<T>::InsertItem(T x,int i)
{
/*i-1的值位于last之间,且顺序表未满*/
if((i-1)<0||i>=MAXSIZE||(i-1)>last||IsFull())
{
return;
}
for(int index = last;index>=(i-1);index--)
{
data[index+1] = data[index];//移动元素
}
data[i-1] = x;
last++;
}//时间复杂度O(n)
template<class T>
void SeqList<T>::InsertTail(T x)
{
if((last==MAXSIZE-1)||IsFull())
{
return;
}
data[++last] = x;
}//时间复杂度O(1),
//删除操作
template<class T>
void SeqList<T>::DeleteItem(T x)
{
int i = LocateItem(x);
if((i-1)>=0)
{
for(int index = i-1;index<=last;index++)
{
data[index] = data[index+1];//移动元素
}
last--;
}
}//时间复杂度O(n),主要用在移动元素上
template<class T>
void SeqList<T>::DeleteTail()
{
if(last>=0)
{
last--;
}
}//时间复杂度O(1)
//定位函数
template<class T>
int SeqList<T>::LocateItem(T x)const
{
for(int i = 0;i<last;i++)
{
if(data[i]==x)
{
return i+1;
}
}
return 0;
}//时间复杂度O(n)
template<class T>
int SeqList<T>::GetLength()const
{
return last+1;
}
template<class T>
void SeqList<T>::SetItem(T x,int i)
{
if((i-1)<0||last==-1||(i-1)>last)
{
return;
}
data[i-1] = x;//修改
}
template<class T>
T &SeqList<T>::GetItem(int i)
{
return (i<0||i>last)?NULL:data[i];
}
template<class T>
int SeqList<T>::IsIn(T x)
{
for(int i = 0;i<=last;i++)
{
if(data[i]==x)
{
return 1;
}
}
return 0;
}
template<class T>
int SeqList<T>::IsEmpty()
{
return last==-1;
}
template<class T>
int SeqList<T>::IsFull()
{
return last == MAXSIZE;
}


main.cpp
#include <iostream>

#include "SSeqList.h"

using namespace std;



int main()

{

SeqList<int> ls(5);
ls.InsertTail(3);
ls.InsertTail(9);
ls.InsertItem(100,2);
ls.SetItem(99,1);
ls.Show();
cout<<"-------------------"<<endl;
ls.DeleteItem(99);
ls.Show();
cout<<"-------------------"<<endl;
ls.DeleteTail();
ls.Show();
if(ls.IsIn(100))
cout<<ls.LocateItem(100)<<endl;

return 0;

}

用命令gcc -o SeqList main.cpp SeqList.cpp SeqList.h编译,报了一大堆错误,下面是错误的一部分。
SeqList.h:33:1: warning: null character(s) ignored
SeqList.h:33:11: warning: null character(s) ignored
SeqList.h:33:13: warning: null character(s) ignored
SeqList.h:33:15: warning: null character(s) ignored
SeqList.h:33:17: warning: null character(s) ignored
SeqList.h:33:19: warning: null character(s) ignored
SeqList.h:33:21: warning: null character(s) ignored
SeqList.h:33:23: warning: null character(s) ignored
SeqList.h:34:1: warning: null character(s) ignored
SeqList.h:34:19: warning: null character(s) ignored
SeqList.h:34:21: warning: null character(s) ignored
SeqList.h:34:23: warning: null character(s) ignored
SeqList.h:34:25: warning: null character(s) ignored
SeqList.h:34:29: warning: null character(s) ignored
SeqList.h:34:31: warning: null character(s) ignored
SeqList.h:34:33: warning: null character(s) ignored
SeqList.h:34:35: warning: null character(s) ignored
SeqList.h:34:37: warning: null character(s) ignored
SeqList.h:34:39: warning: null character(s) ignored
SeqList.h:35:1: warning: null character(s) ignored
SeqList.h:35:19: warning: null character(s) ignored

请大家帮忙看看是什么问题,我放到code::blocks里面去编译也是这么多错误和警告。
...全文
715 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
岁月小龙 2013-05-06
  • 打赏
  • 举报
回复
It looks like you saved the source file as unicode, specifically UTF-16 Try re-saving the file as ascii.
guanzhouxuezi 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dizuo 的回复:]
把SeqList.cpp中的内容放到头文件中去就解决问题了、
或者把SeqList.cpp命名为SeqList.impl,在SeqList.h结尾处加上#include "SeqList.impl"
都可以。
问题解决。请及时结贴~
[/Quote]
明白了,呵呵,多谢
guanzhouxuezi 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dizuo 的回复:]

把SeqList.cpp中的内容放到头文件中去就解决问题了、
或者把SeqList.cpp命名为SeqList.impl,在SeqList.h结尾处加上#include "SeqList.impl"
都可以。
问题解决。请及时结贴~
[/Quote]
我把模板类的声明定义及main函数放在一起的话就编译通过了
guanzhouxuezi 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dizuo 的回复:]

把SeqList.cpp中的内容放到头文件中去就解决问题了、
或者把SeqList.cpp命名为SeqList.impl,在SeqList.h结尾处加上#include "SeqList.impl"
都可以。
问题解决。请及时结贴~
[/Quote]
请教大牛一下,我按照上面的方法做了,第一种还是会报类似的warning,是在main.cpp中。第二种的话,我把模板类的声明的定义跟测试文件放一起的话还是不能通过。
無_1024 2011-06-03
  • 打赏
  • 举报
回复
模板类分离编译
就想叫yoko 2011-06-03
  • 打赏
  • 举报
回复
刚才还一直在回想这个impl后缀名 一时记不清了
其实用别的后缀名也是可以的
这是个约定俗成的东西[Quote=引用 5 楼 dizuo 的回复:]
把SeqList.cpp中的内容放到头文件中去就解决问题了、
或者把SeqList.cpp命名为SeqList.impl,在SeqList.h结尾处加上#include "SeqList.impl"
都可以。
问题解决。请及时结贴~
[/Quote]
就想叫yoko 2011-06-03
  • 打赏
  • 举报
回复
我咋看到都是warning

另外模板的声明和定义写一起吧
ryfdizuo 2011-06-03
  • 打赏
  • 举报
回复
把SeqList.cpp中的内容放到头文件中去就解决问题了、
或者把SeqList.cpp命名为SeqList.impl,在SeqList.h结尾处加上#include "SeqList.impl"
都可以。
问题解决。请及时结贴~

xunxun 2011-06-03
  • 打赏
  • 举报
回复
使用g++编译 不要用gcc
ryfdizuo 2011-06-03
  • 打赏
  • 举报
回复
lz 模板不支持分离模型。老问题了。
guanzhouxuezi 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 nakedavril 的回复:]

编码问题?
[/Quote]
感觉没有问题啊,报错有800+warnings
nakedavril 2011-06-03
  • 打赏
  • 举报
回复
编码问题?

65,210

社区成员

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

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