Struct and chained list

Asked

Viewed 5,915 times

3

I created a person-like structure and I intend to use it in a chained list, but the following errors appear:

'No has no Member named 'data', 'No' has no Member named 'Prox' and unknow type name 'p'.

The program didn’t even run, someone can help me?

The file . h is as follows:

#ifndef Pessoa
#define Pessoa

typedef struct pessoa{
    char nome[25];
    char sobrenome[25];
    int registro;
}Pessoa;

typedef struct no{
    Pessoa dados;
    struct no *prox;
}No;

typedef struct lista{
    No *cabeca;
    No *cauda;
    int tamanho;
}Lista;

No* criaNo(Pessoa p);
Lista* criaLista();
void inserir(Lista *l, Pessoa p, int posicao);
void remover(Lista* l, int registro); //pesquisa registro e remove a pessoa
void imprimeLista(Lista* l);
void destruirLista(Lista* l);

#endif

The file . c is as follows:

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

No* criaNo(Pessoa p)
{
    No* n;
    n=(No*)malloc(sizeof(No));
    n->dados = p;
    n->prox = NULL;
    return n;
}

Lista* criaLista()
{
    Lista* l = (Lista*)malloc(sizeof(Lista));
    l->cabeca = NULL;
    l->cauda = NULL;
    l->tamanho = 0;
    return l;
}
void inserir(Lista *l, Pessoa p, int posicao);

{
     //já está pronta, porém não achei necessário colocá-la
}

void remover(Lista* l, int registro)
{
     //já está pronta, porém não achei necessário colocá-la
}

void imprimeLista(Lista* l)
{
    //já está pronta, porém não achei necessário colocá-la
}

void destruirLista(Lista *l)
{
    //já está pronta, porém não achei necessário colocá-la
}
  • 1

    You saw this ; left in the .c? void inserir(Lista *l, Pessoa p, int posicao);. Was it only when you passed here? Does the program compile? What do you call to test? It only calls the CriaLista()? And the CriaNo() And called too? In which lines do errors occur?

  • 1

    Check if they include the right . h file. It is called "Person. h" in capital letters?

3 answers

2

See, what’s happening is that you’re defining the word Pessoa as an empty string to be replaced by procesator. Because of the #define Pessoa, what is actually being compiled is:

typedef struct pessoa{
    char nome[25];
    char sobrenome[25];
    int registro;
};
typedef struct no{
    dados;
    struct no *prox;
}No;
typedef struct lista{
    No *cabeca;
    No *cauda;
    int tamanho;
}Lista;
No* criaNo( p);
Lista* criaLista();
void inserir(Lista *l, p, int posicao);
void remover(Lista* l, int registro); //pesquisa registro e remove a pessoa
void imprimeLista(Lista* l);
void destruirLista(Lista* l);

See that every occurrence of the word Pessoa disappeared because of his #define Pessoa (I generated this code through the command gcc -E -C -P pessoa.h). If only you’d used #define Pessoa Animal, every occurrence of Pessoa would be replaced by Animal.

I believe what you’re trying to do with #define Pessoa is to use "include Guards". Follow the link (in English) for more useful information on how to do it. In general, be careful that the words you use do not collide with preprocessor macros.

No problem at struct and its typedef have the same name. In your place I would have simply done:

typedef struct Pessoa {
  /*...*/
} Pessoa;

Besides, I’d change the #ifndef/#define Pessoa for #ifndef/#define PESSOA_H

0

My solution to your problem was as follows (Person. h):

#ifndef Pessoa
#define Pessoa

struct pessoa{
    char nome[25];
    char sobrenome[25];
    int registro;
};

typedef struct no{
    struct Pessoa dados;
    struct no *prox;
}No;

typedef struct lista{
    No *cabeca;
    No *cauda;
    int tamanho;
}Lista;

No* criaNo(struct Pessoa p);
Lista* criaLista();
void inserir(Lista *l, struct Pessoa p, int posicao);
void remover(Lista* l, int registro); //pesquisa registro e remove a pessoa
void imprimeLista(Lista* l);
void destruirLista(Lista* l);

#endif

As you can see I removed the typedef of the struct person.

0

The problem was the conflict with your #Define Person...

you can change the directive and put

#ifndef __PESSOA_H__
#define __PESSOA_H__
   // ... aqui seu código usando typedef
#endif

Browser other questions tagged

You are not signed in. Login or sign up in order to post.