user2122810
user2122810

Reputation: 93

Getting a lot of redefinition errors

I am getting a lot of errors, i haven't been able to run this at all, the code isn't mine(code is given online).

Is it my header that's the problem? I know the question is very broad but i'm new to C and these errors are giving me some real trouble. I'm not expecting much but if someone could point me in the correct direction that would be great...

node0.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 */

{
}

node1

#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 */

{
}

node2

#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]);
}

node3

#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]);

}

header file

/* 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);

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

i added the following at the top:

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

Upvotes: 0

Views: 275

Answers (1)

Mathew
Mathew

Reputation: 1798

Haven't looked closely enough to see if it is your only problem, but the redefinition errors are undoubtedly coming from including the header at the top of each c file without any sort of precompiler checking to determine if the header's symbols have already been declared.

Put something like this at the top of the header:

#ifndef _PROG3_H
#define _PROG3_H

And at the bottom of your header file:

#endif

This ensures that if each file is inspected individually by the compiler, the compiler knows you have declared the required symbols. However, if all files are being linked together, you do not get all of the symbols in the header file defined once for each object file you try linking.

EDIT: Now I have looked through some of that code, and, although the header file problem I mentioned definitely needs to be addressed, it is not the only place you are redefining things. Again and again you declare rtpkt as extern in the C files even though you have also included a definition of it from the header file. If it is in the header, you do not want to put it in the C file as well. I see you have distance_table defined numerous times as well. Again, if it is in the header and included that way, do not redefine it in your C file.

Upvotes: 1

Related Questions