【问题标题】:Getting error: expected ‘{’ before ‘(’ token [closed]出现错误:在“(”标记之前应为“{”[关闭]
【发布时间】:2016-05-14 08:00:00
【问题描述】:

我参加了斯坦福的算法第 1 部分课程,我正在尝试在 c 中实现 快速查找算法
下面是我实现快速查找的代码。

  #include <stdio.h>
#include <stdlib.h>
#define n 10
void union (int, int, int *);
void connected(int, int, int *);
int main(){ 

    //initializing array value to it's index value
    int i;
    int id[n];
    for(i = 0 ; i < n;i++)
    {
        id[i] = i;
    }
    union(0,5,id);
    union(3,4,id);
    union(5,8,id);
    connected(3,4,id);
    connected(0,8,id);

}

void union(int a, int b,int *p)
{
    int x, y;
    x = p[b];
    y=  p[a];
    for(int i = 0; i < n; i++)
    {
        if (p[i]==y)
            p[i] = x;
    }

}

void connected(int a, int b,int *p)
{
    if(p[a]==p[b])
        printf("connected");
    else
        printf("not connected");

}

每当我编译程序时,都会产生以下错误。

我已经检查了我的函数原型和声明,但我找不到问题。

quick_find.c:4:12: error: expected ‘{’ before ‘(’ token
 void union (int, int, int *);
            ^
quick_find.c:4:6: error: two or more data types in declaration specifiers
 void union (int, int, int *);
      ^
quick_find.c: In function ‘main’:
quick_find.c:15:7: error: expected ‘{’ before ‘(’ token
  union(0,5,id);
       ^
quick_find.c:16:7: error: expected ‘{’ before ‘(’ token
  union(3,4,id);
       ^
quick_find.c:17:7: error: expected ‘{’ before ‘(’ token
  union(5,8,id);
       ^
quick_find.c: At top level:
quick_find.c:23:11: error: expected ‘{’ before ‘(’ token
 void union(int a, int b,int *p)
           ^
quick_find.c:23:6: error: two or more data types in declaration specifiers
 void union(int a, int b,int *p)

【问题讨论】:

  • 现在看看你在 Stack Overflow 上的帖子:所有unions 都是蓝色的,不像connected 是黑色的。深蓝色表示它是语言关键字,与ifwhile 不同。
  • 您正在编辑代码并使答案无效。这是非常不友好的。请停止这样做; SO 不是语法/语法删除工具。复制/粘贴您的代码,如果您希望发出编辑信号,请附加修改后的副本或提出其他问题!
  • 我会因为拼写错误而关闭它。
  • 编辑旨在添加澄清问题的新信息,不改变它。如果您有不同的问题(尤其是代码更改时),请作为新问题提出。像您一样使用编辑来更改问题非常棒,因为它会使问题中的现有答案不同步,这使得未来的观众很难偶然发现这些答案。编辑时请为他人着想。再见...

标签: c algorithm


【解决方案1】:

unionreserved keyword,不能作为标识符使用。

【讨论】:

  • 更一般地说,您不能将其用作标识符
  • 建议的解决方案:使用 unite 作为函数名,而不是?
【解决方案2】:

注意额外的{

void union{(int a, int b,int *p)
          ^

注意:此答案是在 OP 编辑​​问题以删除错字(在此答案中提到)之前发布的。检查edits 是否清楚。


另一个注意事项: 使用main()的标准定义

int main(void) //if no command line arguments.

【讨论】:

  • 希望你不是认真的...
  • @R.I.P.Seb 啊,这之前代码中的另一个错字。
  • 所以这个问题应该被否决。
  • @AnttiHaapala,是的,OP 在我发布后编辑了问题。
  • 由你决定:) 到目前为止,你有一个赞成票。由于不友好的编辑,我也对这个问题投了反对票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 2012-10-05
  • 2014-01-16
  • 2016-06-18
相关资源
最近更新 更多