读书人

在头文件中宣言该函数头编译出错

发布时间: 2012-09-25 09:55:59 作者: rapoo

在头文件中声明该函数,头编译出错
背景:editPlu + MinGW

SqList.h文件:

#ifndef SQLIST_H_
#define SQLIST_H_

#define NUMBER 5;
#define LIST_MALLOC_SIZE 100;
typedef int ElemType;

typedef struct List
{
ElemType *elem;
int leagth;
int size;
}SqList;

//int SortList(SqList &L);
//int CreateList(SqList &L);
//int DisplayList(SqList &L);
int InitList(SqList &L);

#endif

Main.c文件
#include <stdio.h>
#include "SqList.h"

int main(int argc, char *argv[])
{
int i = 0;
printf("please input 5 number:\n");
struct List l;
InitList(l);
//CreateList(l);
//DisplayList(l);
return 0;
}

InitList.c文件
#include <stdio.h>
#include "SqList.h"

int InitList(SqList &L)
{
L.elem = (ElemType *)malloc(LIST_MALLOC_SIZE * sizeof(ElemType));
L.leagth = 0;
L.size = 100;
return 0;
}

编译结果:D:\learning\coding\2.11\SqList.h:16: error: syntax error before '&' token

[解决办法]

C/C++ code
#define NUMBER 5#define LIST_MALLOC_SIZE 100
[解决办法]
#define最后没分号的
[解决办法]
C语言中没有引用,因此C编译器不认识int InitList(SqList &L)中的&
[解决办法]
1. *.c改成*.cpp, 仅C++引用支持或者
2. 引用&换成等价的指针*
[解决办法]
#define LIST_MALLOC_SIZE 100; => 去掉;

函数声明中的&引用是C++语法 编译时请用g++
[解决办法]
C语言中没有引用的语法,所以你的参数中使用&,编译器是不识别的,认为是错误的。

读书人网 >C++

热点推荐