2015-05-11 2 views
-4

Почему я получаю эти ошибки:'структура ...' не имеет ни одного члена с именем

struct has no members [-Wpedantic]
‘struct cheque’ has no member named ‘refc’
‘struct cheque’ has no member named ‘valor’

и т.д. код, как показано ниже


#ifndef _ITEM_ 
#define _ITEM_ 

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

#define key (a) (a != NULL ? a->refc : "")  
#define less (a,b) (strcmp (a,b)<0) 
#define eq(a,b) (strcmp (a,b) == 0) 
#define NULLitem NULL 

typedef long int* Key;     

typedef struct cheque {     
    int valor 
    long int refe 
    long int refb 
    long int* refc 

}*Item;       

Item newItem (int valor, long int refe, long int refb, long int* refc); 
void deleteItem (Item a); 
void visitItem (Item a); 

#endif 

EDIT:

Теперь я сталкиваюсь с общими ошибками,

Item.c:6:36: error: expected ‘;’, ‘,’ or ‘)’ before ‘refe’ Item newItem (int valor, long item refe, long item refb, long item* refc)^ Item.c: In function ‘visitItem’:

Item.c:32:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=] printf("refe: %d\n", a->refe); ^

Item.c:33:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=] printf("refb: %d\n", a->refb); The code is as below

item.h

#ifndef _ITEM_ 
#define _ITEM_ 

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

#define key (a) (a != NULL ? a->refc : "")  
#define less (a,b) (strcmp (a,b)<0) 
#define eq(a,b) (strcmp (a,b) == 0) 
#define NULLitem NULL 

typedef long int* Key;     

typedef struct cheque {     
    int valor; 
    long int refe; 
    long int refb; 
    long int* refc; 

}*Item;       

Item newItem (int valor, long int refe, long int refb, long int* refc); 
void deleteItem (Item a); 
void visitItem (Item a); 

#endif 

item.c

#include "Item.h" 
#include <stdio.h> 
#include <stdlib.h> 


Item newItem (int valor, long item refe, long item refb, long item* refc) 
{ 

    Item x = (Item) malloc (sizeof(struct cheque)); 

    x->valor = valor; 
    x->refe = refe; 
    x->refb = refb; 
    x->refc = strdup(refc); 

    return x; 
} 


void deleteItem (Item a) 
{ 
    free(a->refc); 
    free(a); 

} 


void visitItem (Item a) 
{ 

    printf("valor: %d\n", a->valor); 
    printf("refe: %d\n", a->refe); 
    printf("refb: %d\n", a->refb); 
    printf("refc: %ld\n", a->refc); 

} 

EDIT_v2

Item newItem (int valor, long int refe, long int refb, long int* refc) 

printf("valor: %d\n", a->valor); 
printf("refe: %ld\n", a->refe); 
printf("refb: %ld\n", a->refb); 
printf("refc: %p\n", a->refc); 

После исправления этих ошибок я получил следующие ошибки:

Item.c: In function ‘newItem’: Item.c:14:2: warning: implicit declaration of function ‘strdup’ [-Wimplicit-function-declaration]

x->refc = strdup(refc); ^Item.c:14:10: warning: assignment makes pointer from integer without a cast [enabled by default] x->refc = strdup(refc);

Item.c: In function ‘visitItem’: Item.c:34:2: warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘long int *’ [-Wformat=] printf("refc: %p\n", a->refc);

Edit_ v3

1 Исправлена ​​проблема Исправление:

x->refc = refc; 

ошибки атм:

Item.c: In function ‘visitItem’: Item.c:34:2: warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘long int *’ [-Wformat=] printf("refc: %p\n", a->refc);

+0

По существу до опечаток: вам не хватает большого количества ';'. Также рассмотрите возможность замены ваших макросов функциями, поскольку последние легче отлаживать. – Bathsheba

ответ

1

Вы забыл полукок Лон:

typedef struct cheque {     
    int valor; 
    long int refe; 
    long int refb; 
    long int* refc;  

}*Item;       
1

Я думаю, ваш код должен выглядеть

typedef struct cheque {     
    int valor; 
    long int refe; 
    long int refb; 
    long int* refc; 

}*Item; 

с ; с.


EDIT:

Я думаю, что это еще одна опечатка.

Item newItem (int valor, long item refe, long item refb, long item* refc) 

следует читать

Item newItem (int valor, long int refe, long int refb, long int* refc) 

Затем

printf("refe: %d\n", a->refe); 
printf("refb: %d\n", a->refb); 
printf("refc: %ld\n", a->refc); 

оказывает неправильные спецификаторов формат, который, в свою очередь идет призыв undefined behaviour.

Правильный формат для long int - %ld, а для long int * - %p.

Также, пожалуйста, do not cast возвращаемое значение malloc() и семья в C.

+0

Черт, вы правы, такая ошибка новобранец – Miguel

+0

@Miguel Ничего, бывает. :-) –

+0

Спасибо :) Можете ли вы помочь мне с моими новыми erros: /? Я новичок в указателях, структурах и материалах FIFO в C .. – Miguel

Смежные вопросы