在以下代码中找不到错误: - 在try()之前预期的unqualified-id

时间:2013-12-29 05:25:38

标签: c++

#include<stdio.h>
#include<string.h>
void try(char s[])
{
    if(strlen(s)>5)
    {
        puts("Error\n");
    }
}
int main()
{
    char string[10];
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",&string);
        try(string);
    }

    return 0;
}

仍然无法找到错误...尝试是一个简单的功能,而且我一直在创建一个函数并调用它。编译器给出错误 - (在'try'之前预期的unqualified-id)

2 个答案:

答案 0 :(得分:5)

我怀疑你是在尝试将代码编译为C ++而不是C.在C ++中,tryreserved word(它用于异常处理)。

$ gcc test.c
$ g++ test.c
test.c:3:6: error: expected unqualified-id before 'try'

您可以使用-x明确设置语言(使用gccg++):

$ gcc -x c test.c
$ gcc -x c++ test.c
test.c:3:6: error: expected unqualified-id before 'try'

答案 1 :(得分:2)

尝试在调用中添加-x c或将文件重命名为main.c。可能是gcc因某种原因选择将C文件编译为C ++。