0

我在尝试使用 GCC 编译我的程序时收到这些错误,我不确定是什么导致了它们。

functions.h:21: error: expected ')' before '[' token
functions.h:22: error: expected ')' before '[' token
functions.h:23: error: expected ')' before '[' token
functions.h:25: error: expected ')' before '[' token
functions.h:26: error: expected ')' before '[' token
functions.h:27: error: expected ')' before '[' token

我的程序在 Visual Studio 2012 中编译得很好。

这是似乎导致错误的头文件。

struct subject
{
    char year[5];
    char session;
    char code[8];
    char credit[3];
    char mark[4];
};

struct data
{
    char name[30];
    char id[30];
    char cc[30]; 
    char course[80];
    struct subject subjects[30];
    int gpa;
};

void displayRecord(data [], int);
int nameSearch(data [], char [], int [], int);
void editRecord(data [], int, int);
char getChar(const char [], int);
int getData(data []);
void displayData(data []);
void deleteRecord(data [], int, int);

我正在像这样调用编译器:

gcc -o 测试函数.cpp 函数.h main.cpp

我很难过,所以任何帮助将不胜感激!

4

2 回答 2

5

问题是您正在传递functions.h给编译器。那是一个包含文件,您应该让两个 .cpp 文件包含它。无需在编译器的命令行调用中传递它。只需functions.h从 gcc 的命令行调用中删除。

由于这是 C++,因此您应该使用 g++ 而不是 gcc 进行编译。由于您使用gcc了 ,因此编译器将functions.h其视为 C,并且代码无效 C。

所以,我认为你的编译应该是

g++ -o 测试函数.cpp main.cpp
于 2013-08-26T14:07:46.897 回答
5

我的通灵调试能力告诉我,您的 Visual Studio 将代码编译为 C++,而 gcc 将其编译为 C。由于您在函数参数中缺少structbefore 关键字data,C 编译器不知道该怎么做。尝试通过 g++ 而不是 gcc 运行它(并可能确保包含源文件的扩展名是.C.cpp.

于 2013-08-26T14:04:18.923 回答