首页 文章

'struct ...'没有名为[closed]的成员

提问于
浏览
-4

为什么我一直收到这个错误:

struct没有成员[-Wpedantic]'struct check'没有名为'refc'的成员'struct check'没有名为'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

编辑:

现在我正面临着以下错误,

Item.c:6:36:错误:在'refe'之前预期';',','或')'项目newItem(int valor,long item refe,long item refb,long item * refc)^ Item.c :在函数'visitItem'中:Item.c:32:2:警告:格式'%d'需要类型为'int'的参数,但参数2的类型为'long int'[-Wformat =] printf(“refe:% d \ n“,a-> refe); ^ Item.c:33:2:警告:格式'%d'需要类型为'int'的参数,但参数2的类型为'long int'[-Wformat =] printf(“refb:%d \ n”,a - > REFB);代码如下

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:在函数'newItem'中:Item.c:14:2:warning:函数'strdup'的隐式声明[-Wimplicit-function-declaration] x-> refc = strdup(refc); ^ Item.c:14:10:警告:赋值从整数中生成没有强制转换的指针[默认启用] x-> refc = strdup(refc); Item.c:在函数'visitItem'中:Item.c:34:2:警告:格式'%p'需要类型为'void *'的参数,但参数2的类型为'long int *'[-Wformat =] printf (“refc:%p \ n”,a-> refc);

Edit_ v3

1问题修正纠正:

x->refc = refc;

错误atm:

Item.c:在函数'visitItem'中:Item.c:34:2:警告:格式'%p'需要'void *'类型的参数,但参数2的类型为'long int *'[-Wformat =] printf(“refc:%p \ n”,a-> refc);

2 回答

  • 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;
    

    ; .


    编辑:

    我想,这是另一个错字 .

    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 家族的返回值 .

相关问题