c++ - 得到很多重定义错误

标签 c++ c

我遇到了很多错误,我根本无法运行它,代码不是我的(代码是在线提供的)。

是我的标题有问题吗?我知道这个问题很广泛,但我是 C 的新手,这些错误给我带来了一些真正的麻烦。我期望不高,但如果有人能给我指出正确的方向,那就太好了……

节点0.c

   #include <stdio.h>
#include "prog3.h"
extern struct rtpkt {
  int sourceid;       /* id of sending router sending this pkt */
  int destid;         /* id of router to which pkt being sent 
                         (must be an immediate neighbor) */
  int mincost[4];    /* min cost to node 0 ... 3 */
  };

extern int TRACE;
extern int YES;
extern int NO;

struct distance_table 
{
  int costs[4][4];
} dt0;


/* students to write the following two routines, and maybe some others */

void rtinit0() 
{

}


void rtupdate0(rcvdpkt)
  struct rtpkt *rcvdpkt;
{

}


printdt0(dtptr)
  struct distance_table *dtptr;

{
  printf("                via     \n");
  printf("   D0 |    1     2    3 \n");
  printf("  ----|-----------------\n");
  printf("     1|  %3d   %3d   %3d\n",dtptr->costs[1][1],
     dtptr->costs[1][2],dtptr->costs[1][3]);
  printf("dest 2|  %3d   %3d   %3d\n",dtptr->costs[2][1],
     dtptr->costs[2][2],dtptr->costs[2][3]);
  printf("     3|  %3d   %3d   %3d\n",dtptr->costs[3][1],
     dtptr->costs[3][2],dtptr->costs[3][3]);
}

linkhandler0(linkid, newcost)   
  int linkid, newcost;

/* called when cost from 0 to linkid changes from current value to newcost*/
/* You can leave this routine empty if you're an undergrad. If you want */
/* to use this routine, you'll need to change the value of the LINKCHANGE */
/* constant definition in prog3.c from 0 to 1 */

{
}

节点1

#include <stdio.h>
#include "prog3.h"
extern struct rtpkt {
  int sourceid;       /* id of sending router sending this pkt */
  int destid;         /* id of router to which pkt being sent 
                         (must be an immediate neighbor) */
  int mincost[4];    /* min cost to node 0 ... 3 */
  };


extern int TRACE;
extern int YES;
extern int NO;

int connectcosts1[4] = { 1,  0,  1, 999 };

struct distance_table 
{
  int costs[4][4];
} dt1;


/* students to write the following two routines, and maybe some others */


rtinit1() 
{

}


rtupdate1(rcvdpkt)
  struct rtpkt *rcvdpkt;

{

}


printdt1(dtptr)
  struct distance_table *dtptr;

{
  printf("             via   \n");
  printf("   D1 |    0     2 \n");
  printf("  ----|-----------\n");
  printf("     0|  %3d   %3d\n",dtptr->costs[0][0], dtptr->costs[0][2]);
  printf("dest 2|  %3d   %3d\n",dtptr->costs[2][0], dtptr->costs[2][2]);
  printf("     3|  %3d   %3d\n",dtptr->costs[3][0], dtptr->costs[3][2]);

}



linkhandler1(linkid, newcost)   
int linkid, newcost;   
/* called when cost from 1 to linkid changes from current value to newcost*/
/* You can leave this routine empty if you're an undergrad. If you want */
/* to use this routine, you'll need to change the value of the LINKCHANGE */
/* constant definition in prog3.c from 0 to 1 */

{
}

节点2

#include <stdio.h>
#include "prog3.h"
extern struct rtpkt {
  int sourceid;       /* id of sending router sending this pkt */
  int destid;         /* id of router to which pkt being sent 
                         (must be an immediate neighbor) */
  int mincost[4];    /* min cost to node 0 ... 3 */
  };

extern int TRACE;
extern int YES;
extern int NO;

struct distance_table 
{
  int costs[4][4];
} dt2;


/* students to write the following two routines, and maybe some others */

void rtinit2() 
{
}


void rtupdate2(rcvdpkt)
  struct rtpkt *rcvdpkt;

{

}


printdt2(dtptr)
  struct distance_table *dtptr;

{
  printf("                via     \n");
  printf("   D2 |    0     1    3 \n");
  printf("  ----|-----------------\n");
  printf("     0|  %3d   %3d   %3d\n",dtptr->costs[0][0],
     dtptr->costs[0][1],dtptr->costs[0][3]);
  printf("dest 1|  %3d   %3d   %3d\n",dtptr->costs[1][0],
     dtptr->costs[1][1],dtptr->costs[1][3]);
  printf("     3|  %3d   %3d   %3d\n",dtptr->costs[3][0],
     dtptr->costs[3][1],dtptr->costs[3][3]);
}

节点3

#include <stdio.h>
#include "prog3.h"
extern struct rtpkt {
  int sourceid;       /* id of sending router sending this pkt */
  int destid;         /* id of router to which pkt being sent 
                         (must be an immediate neighbor) */
  int mincost[4];    /* min cost to node 0 ... 3 */
  };

extern int TRACE;
extern int YES;
extern int NO;

struct distance_table 
{
  int costs[4][4];
} dt3;

/* students to write the following two routines, and maybe some others */

void rtinit3() 
{
}


void rtupdate3(rcvdpkt)
  struct rtpkt *rcvdpkt;

{

}


printdt3(dtptr)
  struct distance_table *dtptr;

{
  printf("             via     \n");
  printf("   D3 |    0     2 \n");
  printf("  ----|-----------\n");
  printf("     0|  %3d   %3d\n",dtptr->costs[0][0], dtptr->costs[0][2]);
  printf("dest 1|  %3d   %3d\n",dtptr->costs[1][0], dtptr->costs[1][2]);
  printf("     2|  %3d   %3d\n",dtptr->costs[2][0], dtptr->costs[2][2]);

}

头文件

/* libraries */
#include <stdlib.h>

/* definitions */

#define INF 999

#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif

struct rtpkt {
    int sourceid;       /* id of sending router sending this pkt */
    int destid;         /* id of router to which pkt being sent 
                                                 (must be an immediate neighbor) */
    int mincost[4];    /* min cost to node 0 ... 3 */
};

struct event {
   float evtime;           /* event time */
   int evtype;             /* event type code */
   int eventity;           /* entity where event occurs */
   struct rtpkt *rtpktptr; /* ptr to packet (if any) assoc w/ this event */
   struct event *prev;
   struct event *next;
};

struct distance_table 
{
    int costs[4][4];
    int link[4];
};

/* forward declarations */
void init();
void tolayer2(struct rtpkt packet);
void insertevent(struct event *p);
void rtupdate0(struct rtpkt *rcvdpkt);
void rtupdate1(struct rtpkt *rcvdpkt);
void rtupdate2(struct rtpkt *rcvdpkt);
void rtupdate3(struct rtpkt *rcvdpkt);
void linkhandler0(int linkid, int newcost);
void linkhandler1(int linkid, int newcost);
void rtinit0();
void rtinit1();
void rtinit2();
void rtinit3();
void sendpkt0();
void sendpkt1();
void sendpkt2();
void sendpkt3();
void printdt0(struct distance_table *dtptr);
void printdt1(struct distance_table *dtptr);
void printdt2(struct distance_table *dtptr);
void printdt3(struct distance_table *dtptr);

主要: http://gaia.cs.umass.edu/kurose/network/prog3.c

我在顶部添加了以下内容:

#include "stdafx.h"
#include "prog3.h"

最佳答案

没有足够仔细地查看它是否是您唯一的问题,但重新定义错误无疑是由于在每个 c 文件的顶部包含 header 而没有任何类型的预编译器检查以确定 header 的符号是否已经存在已宣布。

在页眉顶部放置类似这样的内容:

#ifndef _PROG3_H
#define _PROG3_H

在头文件的底部:

#endif

这确保如果编译器单独检查每个文件,编译器就会知道您已经声明了所需的符号。但是,如果所有文件都链接在一起,您将无法获得头文件中为您尝试链接的每个目标文件定义一次的所有符号。

编辑: 现在我已经查看了其中的一些代码,虽然我提到的头文件问题确实需要解决,但它并不是您要重新定义内容的唯一地方。你一次又一次地在 C 文件中将 rtpkt 声明为 extern,即使你还从头文件中包含了它的定义。如果它在头文件中,您也不想将它放在 C 文件中。我看到您也多次定义了 distance_table。同样,如果它在 header 中并以这种方式包含,请不要在您的 C 文件中重新定义它。

关于c++ - 得到很多重定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15916358/

相关文章:

c++ - C++ 中的基本运算符重载语法

c++ - 使用 Boost.Phoenix 有什么好处?

c - 还有其他方法可以编译使用 R api 的 c 代码吗?

c - 生成 ftok() key 的公式是什么?

c++ - 具有分层访问控制的存储库的 SVN 或 Git

c++ - 使用其他目录中的库构建项目。 Windows , MinGW

c++ - Opengl 3.3 不绘制任何东西。使用 GLSL 330 核心

c# - 向 C# 公开 C++ API

c - 在 C 中返回反转字符串的函数 - 分配

c - 实现 scanf 的替代方案