C compiling - "undefined reference to"?

Kat*_*rek 8 c data-structures

I am making a reliable data transfer protocol and have the function prototype

void tolayer5(int, char data[]);
Run Code Online (Sandbox Code Playgroud)

With the structs

struct msg {
  char data[20];
};

struct pkt {
   int seqnum;
   int acknum;
   int checksum;
   char payload[20];
};
Run Code Online (Sandbox Code Playgroud)

And when I call the function in this format:

tolayer5(A, packet.payload);
Run Code Online (Sandbox Code Playgroud)

Where A is an int and packet.payload is a struct pkt, I get the error "undefined reference to 'tolayer5(int, char*)'. Can you help me see what I'm missing here?

void tolayer5(int AorB, char data[])
{
  int i;
  if (TRACE>2)
  {
     printf("TOLAYER5: data received:");
     for (i=0; i<20; i++)
        printf("%c",data[i]);
     printf("\n");
  }
}
Run Code Online (Sandbox Code Playgroud)

谢谢大家帮忙解决原始问题!:)然而,当我试图修复那个时,我遇到了一个无限循环,我认为这与我在一个数组中错误地解决字符有关(这已经有一段时间了,因为我这样做了C.你能帮我找到吗我在哪里创建一个无限循环?

我已经将上面的代码更新为我正在使用的内容.注意主要的变化是我的功能:

void tolayer5(int AorB, char data[])
Run Code Online (Sandbox Code Playgroud)

这一行在函数内部:printf("%c",msgReceived.data[i]);从现在开始它只是:

printf("%c",data[i]);
Run Code Online (Sandbox Code Playgroud)

tri*_*tan 12

似乎你需要链接到实现tolayer5()的obj文件

更新:您的函数声明与实现不匹配:

      void tolayer5(int AorB, struct msg msgReceived)
      void tolayer5(int, char data[])
Run Code Online (Sandbox Code Playgroud)

因此编译器会将它们视为两个不同的函数(您使用的是c ++).它找不到你在main()中调用的实现.