plik naglowkowy struktura

0

Cześć, nie mogę sobie poradzić z strukturą w pliku nagłówkowym. Tak mi się wydaje, że to tam leży problem.

funkcje.h

 
struct komunikat
{
	long int typ;
	char text[MAX_TEXT];
};

void dostales_wiad(int, struct komunikat*, int); 
void wyslij_wiad(int, struct komunikat*);

funkcje.c

 
#include "funkcje.h"

void dostales_wiad(int kolejka, komunikat* wiad, int r)
{
	int blad = msgrcv(kolejka, wiad, max_rozmiar, r, 0); 
}

void wyslij_wiad(int kolejka, komunikat* wiad)
{
	int blad = msgsnd(kolejka, &wiad, max_rozmiar, 0);
}

wywala blad

 
make all
gcc -o klient.out klient.c -lm
In file included from funkcje.h:4:0,
                 from klient.c:10:
funkcje.c:27:33: error: unknown type name ‘komunikat’
 void dostales_wiad(int kolejka, komunikat* wiad, int r)
                                 ^
In file included from funkcje.h:4:0,
                 from klient.c:10:
funkcje.c:33:31: error: unknown type name ‘komunikat’
 void wyslij_wiad(int kolejka, komunikat* wiad)
                               ^
makefile:7: polecenia dla obiektu 'klient' nie powiodły się
make: *** [klient] Błąd 1

Z góry dziękuję za pomoc.

0

musisz miec include w pliku c bo nie zna definicji struktury

1

w funkcje.c brakuje Ci
struct
powinno byc tak samo jak masz w h

0

jak poprawie funkcje.h, to wywala jeszcze więcej błędów

 
#include "funkcje.h"
 
void dostales_wiad(int kolejka, struct komunikat* wiad, int r)
{
    int blad = msgrcv(kolejka, wiad, max_rozmiar, r, 0); 
}
 
void wyslij_wiad(int kolejka, struct komunikat* wiad)
{
    int blad = msgsnd(kolejka, &wiad, max_rozmiar, 0);
}
 
make all
gcc -o klient.out klient.c -lm
In file included from funkcje.h:4:0,
                 from klient.c:10:
funkcje.c:27:40: warning: ‘struct komunikat’ declared inside parameter list
 void dostales_wiad(int kolejka, struct komunikat* wiad, int r)
                                        ^
funkcje.c:27:40: warning: its scope is only this definition or declaration, which is probably not what you want
funkcje.c: In function ‘dostales_wiad’:
funkcje.c:29:35: error: ‘max_rozmiar’ undeclared (first use in this function)
  int blad = msgrcv(kolejka, wiad, max_rozmiar, r, 0); //pobranie komunikatu od identyfikatora kolejki i umieszczenie go tam gdzie wskazuje wiad
                                   ^
funkcje.c:29:35: note: each undeclared identifier is reported only once for each function it appears in
In file included from funkcje.h:4:0,
                 from klient.c:10:
funkcje.c: At top level:
funkcje.c:33:38: warning: ‘struct komunikat’ declared inside parameter list
 void wyslij_wiad(int kolejka, struct komunikat* wiad)
                                      ^
funkcje.c: In function ‘wyslij_wiad’:
funkcje.c:35:36: error: ‘max_rozmiar’ undeclared (first use in this function)
  int blad = msgsnd(kolejka, &wiad, max_rozmiar, 0);
                                    ^
In file included from klient.c:10:0:
funkcje.h: At top level:
funkcje.h:15:6: error: conflicting types for ‘dostales_wiad’
 void dostales_wiad(int, struct komunikat*, int); //odebranie wiadomosci
      ^
In file included from funkcje.h:4:0,
                 from klient.c:10:
funkcje.c:27:6: note: previous definition of ‘dostales_wiad’ was here
 void dostales_wiad(int kolejka, struct komunikat* wiad, int r)
      ^
In file included from klient.c:10:0:
funkcje.h:16:6: error: conflicting types for ‘wyslij_wiad’
 void wyslij_wiad(int, struct komunikat*); //wysyla wiadomosc
      ^
In file included from funkcje.h:4:0,
                 from klient.c:10:
funkcje.c:33:6: note: previous definition of ‘wyslij_wiad’ was here
 void wyslij_wiad(int kolejka, struct komunikat* wiad)
      ^
makefile:7: polecenia dla obiektu 'klient' nie powiodły się
make: *** [klient] Błąd 1

0

Jeżeli usunę strukturę z pliku funkcje.h i dodam ją do pliku funkcje.c to wszystko działa, ale to chyba nie tak powinno być..

0

Ja osobiście zrobiłbym to troszeczkę inaczej:
funkcje.h:

#ifndef funkcjeH
#define funkcjeH

typedef struct
{
    long int typ;
    char text[MAX_TEXT];
} komunikat;

void dostales_wiad(int, komunikat*, int);
void wyslij_wiad(int, komunikat*);

#endif

funkcje.c

#include "funkcje.h"

void dostales_wiad(int kolejka, komunikat* wiad, int r)
{
    int blad = msgrcv(kolejka, wiad, max_rozmiar, r, 0);
}

void wyslij_wiad(int kolejka, komunikat* wiad)
{
    int blad = msgsnd(kolejka, &wiad, max_rozmiar, 0);
}

O wiele wygodniejsze niż ciągłe dopisywanie struct w funkcjach

https://ideone.com/CzlnpD

0

a co to jest max_rozmiar?

1 użytkowników online, w tym zalogowanych: 0, gości: 1