0

In my code (using code blocks) it kept saying multiple definition in every function in wagon.c

I try change so much var in .h and main but it useless. I'm not that good in English nor in code ^^ so please any help or some sites to learn this chapter to improve my skills.

I have the .h and its good

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wagon.h"

void saisir_wagon(wagon *w) //here there is a multipel definition how``
{
  printf("donner un numero\n");
  scanf("%d", &w->num);
  do {
    printf("donner le type\n");
    gets(w->type);
  } while ((strcmp(w->type, "wagon") != 0)
      && (strcmp(w->type, "locomotive" != 0)));
  do {
    printf("donner l etat \n");
    scanf("%d", &w->ok);
  } while ((w->ok = !0) && (w->ok = !1));
}

LIST ajout_deb(wagon w, LIST l) {
  struct cellule *nouv;

  nouv = malloc(sizeof(wagon));

  if (l != NULL) {
    nouv->w = w;
    nouv->suiv = l;
  } else {
    nouv->suiv = NULL;
    l = nouv;
  }

  return l;
}

LIST ajout_queue(wagon W, LIST l) {
  struct cellule *nouv, *parc;

  nouv = malloc(sizeof(cellule));
  nouv->w = W;
  nouv->suiv = NULL;

  if (l == NULL) {
    l = nouv;
  } else {
    parc = l;
    while (parc->suiv != NULL) {
      parc = parc->suiv;
    }
    parc->suiv = nouv;
  }
  return (l);
}

void parkour_l(LIST l) {
  struct cellule *tmp = l;
  if (l == NULL)
    printf("liste vide /n");
  else {
    while (tmp != NULL) {
      printf("le numero :%d /n", tmp->w.num);
      printf("le type :%s /n", tmp->w.type);
      printf("l'etat: %d /n", tmp->w.ok);
      tmp = tmp->suiv;
    }
  }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    Move function definitions from the header in a C module. – Vlad from Moscow Feb 09 '22 at 18:20
  • Given that `#include "wagon.h"`, are you showing the header (`.h` file) or the `.c` file? Please post both. Does the header have [include guards](https://stackoverflow.com/questions/27810115/what-exactly-do-c-include-guards-do)? – Bob__ Feb 09 '22 at 18:23

1 Answers1

0

The reason of the error is that you placed function definitions in a header that is included more than one module. As a result the functions are defined more than one time.

Remove the function definitions from the header and place them in a module.

The header should have only function declarations without their definitions if the functions are declared without the function specifier inline.

Pay attention to that the function gets is unsafe and is not supported by the C Standard.

Also in the condition expression

 } while ((w->ok = !0) && (w->ok = !1));

it seems there is a typo.

Maybe you mean

 } while ((w->ok != 0) && (w->ok != 1));

It seems there is another typo

  struct cellule *nouv;

  nouv = malloc(sizeof(wagon));

Maybe you need to write

  nouv = malloc(sizeof( struct cellule ));

And maybe this if-else statement

  if (l != NULL) {
    nouv->w = w;
    nouv->suiv = l;
  } else {
    nouv->suiv = NULL;
    l = nouv;
  }

should be rewritten like one if statement.

  if (l != NULL) {
    nouv->w = w;
    nouv->suiv = l;
    l = nouv;
  }

That is it seems there are several problems with your function definitions.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335