【问题标题】:Function error 'redefined' [closed]函数错误“重新定义”[关闭]
【发布时间】:2017-02-03 21:19:21
【问题描述】:

您好,我收到关于我的函数的错误 - 说它未定义,我无法理解编译器真正想要什么。不,我不能使用数组,而且我在制作全局变量时遇到了麻烦,所以必须这样做。当我运行它时,会产生以下错误消息:

错误 4 错误 C2371:'moveHandler':重新定义;不同的基本类型 c:\users\owner\documents\visual studio 2012\projects\project36\project36\source.c 120 1 Project36

函数定义:

   void moveHandler( char source, char destination, char *pa, char *pb, char *pc, char *pd, char *pe, char *pf,
                     char *pg, char *ph, char *pi, char *pj, char *pk, char *pl, char *pm, char *pn, char *po ) {
                         char temp;

                         if ( source == 'D' && destination == 'A' ) {
                             temp = *pa;
                             *pa = *pd;
                             *pd = temp;
                         }
    return;
    }

函数调用:

moveHandler( sourcePiece, destination, &pa, &pb, &pc, &pd, &pe, &pf, &pg, &ph, &pi, &pj, &pk, &pl, &pm, &pn, &po); 

【问题讨论】:

  • 天哪...这是我见过的最长的参数列表..
  • 15 个板件 + 2 个其他 gen 参数,抱歉。
  • 听说过数组和/或结构吗?
  • 我无法将它们用于此分配,并且无法获取全局变量。
  • 你是在定义之前调用这个函数吗?也许在调用之前声明函数可能会有所帮助。

标签: c function arguments


【解决方案1】:

您在 C 代码中调用了一个未声明的函数。确保在尝试调用 C 函数(带有原型)之前声明它们。

在提问之前尝试搜索 SO 是有意义的。搜索“重新定义;不同的基本类型”会立即为您提供各种各样的答案。

【讨论】:

    【解决方案2】:

    如果您调用编译器找不到或不知道的函数,它会假定它只是从其他地方链接到的(例如,链接器将处理的其他库),并返回 int .

    如果您稍后在编译器看到它被调用(但未在您的代码中定义或声明)之后定义函数,编译器将抛出重新定义错误,因为它已经决定该函数必须存在于外部并返回一个int

    要解决此问题,请在定义任何函数之前声明所有函数:

    不好:

    #include <stdio.h>
    int main()
    {
         printf("%d\n", add2(5, 6));
         return 0;
    }
    int add2(int a, int b)
    {
         return a + b;
    }
    

    好:

    #include <stdio.h>
    int add2(int a, int b);
    int main()
    {
         printf("%d\n", add2(5, 6));
    }
    int add2(int a, int b)
    {
         return a + b;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      相关资源
      最近更新 更多