头文件的编写和引用

      我用的是 Dev-c++  编写头文件

首先写头文件里面的函数,

然后保存,记得后缀写  .h   就行

例:(顺序表的头文件)

#define MAXSize 50
#define OVERFLOW -2
#define ElemType int
using namespace std;
typedef struct List{
ElemType *elem;
int length;
}SqList;
void InitList(SqList &L)
{ //构造一个空的顺序表
L.elem=new ElemType[MAXSize];
if (!L.elem)
exit(OVERFLOW);
L.length=0;
}
void ClearList(SqList &L)
{ //清空线性表,不销毁
L.length=0;
}
int ListLength (SqList L)
{ //求线性表长度
return (L.length);
}
bool ListInsert (SqList &L, int i , ElemType e)
{ //在线性表L中第i个数据元素之前插入新数据元素e
int j;
if (i<1||i>L.length+1)
return false;
if (L.length>=MAXSize)
return false;
for (j=L.length;j>=i;--j)
L.elem[j]=L.elem[j-1];
L.elem[i-1]=e;
++L.length;
return true;
}
ElemType GetElem(SqList L, int i)
{ //在线性表L中求序号为i的元素,该元素作为函数返回值
if (i<1||i>L.length){
printf("i不在[1..n]范围内");
exit(OVERFLOW);
}
return (L.elem[i-1]);
}
#define MAXSize 50
#define OVERFLOW -2
#define ElemType int
using namespace std;
typedef struct List{
ElemType *elem;
int length;
}SqList;
void InitList(SqList &L)
{ //构造一个空的顺序表
L.elem=new ElemType[MAXSize];
if (!L.elem)
exit(OVERFLOW);
L.length=0;
}
void ClearList(SqList &L)
{ //清空线性表,不销毁
L.length=0;
}
int ListLength (SqList L)
{ //求线性表长度
return (L.length);
}
bool ListInsert (SqList &L, int i , ElemType e)
{ //在线性表L中第i个数据元素之前插入新数据元素e
int j;
if (i<1||i>L.length+1)
return false;
if (L.length>=MAXSize)
return false;
for (j=L.length;j>=i;--j)
L.elem[j]=L.elem[j-1];
L.elem[i-1]=e;
++L.length;
return true;
}
ElemType GetElem(SqList L, int i)
{ //在线性表L中求序号为i的元素,该元素作为函数返回值
if (i<1||i>L.length){
printf("i不在[1..n]范围内");
exit(OVERFLOW);
}
return (L.elem[i-1]);
}

然后保存

头文件就写好了,然后引用 

方法1:

  #include"E:List.h"             //   #include"  头文件存放路径+头文件名"

方法2:

  将编写的头文件 和  正准备调用          该头文件      的项目文件放在同一个文件夹里面

原文地址:https://www.cnblogs.com/q1204675546/p/9891181.html