3

This code is giving me this error, which I don't understand. I can't even run the program. Can you help me to fix this error, please? If you don't understand anything in the code, say it.

error C2040: 'CancelarPedido' : 'ppedido (ppedido)' differs in levels of indirection from 'int ()'

printf("\nIntroduza opcao:");
    scanf("%d",&opc);

    switch(opc){
        case 1: lista = NovoPedido(lista);break;
        case 2: lista = CancelarPedido(lista);break;
        case 3: printf("Falta implementar a funcao.");break;
        case 4: printf("Falta implementar a funcao.");break;
    }
    }while(opc!=5);

    return lista;
}


ppedido CancelarPedido(ppedido lista)
{
    ppedido actual, anterior = NULL;

    char id[5];

    actual = lista;

    if(lista == NULL)
        printf("\nNao ha pedidos na fila de espera...");
    else
    {
        printf("\nIntroduza o ID do pedido que pretende cancelar: ");
        scanf("%s", id);

        while(actual != NULL && ((strcmp(actual->id, id)) != 0)){
            anterior = actual;
            actual = actual->prox;
        }
        if(actual == NULL){
            printf("\nERRO - Nao existe nenhum pedido com o ID introduzido.");
            return lista;
        }
        if(anterior == NULL){
            lista = actual->prox;
            printf("\nPedido cancelado com sucesso...");
        }
        else{
            anterior->prox = actual->prox;
            printf("\nPedido cancelado com sucesso...");
        }
        free(actual);
        return lista;
    }
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
Marco
  • 625
  • 3
  • 10
  • 30
  • ppedido is a list type, if that's what you mean. – Marco Jun 23 '12 at 04:31
  • What data type is the first piece of your code declared to return? – Etheryte Jun 23 '12 at 04:32
  • typedef struct pedido pedido, *ppedido; struct pedido{ char id[5]; int prioridade; int mesa, n_pratos; struct prato *prato[TAM]; ppedido prox; }; struct prato{ char id[5]; }; – Marco Jun 23 '12 at 04:36
  • Difficult to answer properly without an [SSCCE](http://www.sscce.org/). – DCoder Jun 23 '12 at 04:37
  • I didn't have any difficulty answering it. – Jim Balter Jun 23 '12 at 04:42
  • I have struct on a "header file" with a pointer to it, the ppedido. Then declare the list in the main function, and sent to another file where is this code that I present. The problem is that I can as well send the list of function to function, but when I call this function, the program shows me this error. I have no idea why. – Marco Jun 23 '12 at 04:43
  • Try reading my answer and then you will have an idea why. Without a declaration of CancelarPedido in scope, it defaults to int CancelarPedido() – Jim Balter Jun 23 '12 at 04:46
  • Lessons in understanding code: The code refers to a pedido, which is Portuguese for "order" (and there are functions for new order and cancel order). ppedido is obviously a pointer to a struct that represents an order. – Jim Balter Jun 23 '12 at 04:53

1 Answers1

9

You are calling CancelarPedido before you declared it. You need to reorder the code or add a forward declaration for CancelarPedido.

Without a declaration of CancelarPedido in scope, it defaults to int CancelarPedido(). You get the error message because lista is declared to be a pointer but CancelarPedido is declared to return an int.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • 1
    Could be predeclared earlier in the code, we only have a fragment of the code. Talking of which, it would be more easy to help if we saw more code. – Etheryte Jun 23 '12 at 04:33
  • @Nit Um, if it were predeclared then we wouldn't see that error message. – Jim Balter Jun 23 '12 at 04:41
  • So how do I declare in order to return a list? Thank's one more time. – Marco Jun 23 '12 at 04:56
  • I'm not clear on your question. You have declared CancelarPedido as returning a ppedido, which is how your lista is declared, right? So the only problem is that there's no declaration of CancelarPedido in scope when you call it. You can add `ppedido CancelarPedido(ppedido lista);` to your header file. – Jim Balter Jun 23 '12 at 05:00