0
我有一個問題不兼容的返回類型錯誤是什麼意思?我不斷收到一個錯誤,說這個,我改變了我的代碼多次,什麼也沒有。任何人都可以幫助我,謝謝。不兼容的返回類型
即時得到錯誤的:
circular_list.c: In function ‘circ_list_iterator’:
circular_list.c:116:(return lst ? NEXT(lst) : NULL;)error: incompatible types in return
circular_list.c:118:(return (lastreturn == lst) ? NULL : NEXT(lastreturn);)error: incompatible types in return
circular_list.c:119:(})warning: control reaches end of non-void function
circular_list.c: In function ‘circ_length’:
circular_list.c:127:(while((lastreturn = circ_list_iterator(lst, lastreturn)) != NULL))error: incompatible types in assignment
circular_list.c: In function ‘nth_node’:
circular_list.c:136:(return NULL;)error: incompatible types in return
circular_list.c:138:(return lst;)error: incompatible types in return
circular_list.c:145:(return (number != 0) ? NULL : tmp;)error: incompatible types in return
circular_list.c
#include <stdio.h>
#include <stdlib.h>
#include "globals.h"
#include "circular_list.h"
typedef struct node *list_rep;
typedef struct node {
generic_ptr datapointer;
list_rep next;
} node;
#define DATA(lst) (((list_rep) (lst))->datapointer)
#define NEXT(lst) (((list_rep) (lst))->next)
#define TOLIST(list_rep) ((list) (list_rep))
#define TOREP(lst) ((list_rep)(lst))
#define TOLISTPTR(list_rep_ptr) ((list *) (list_rep_ptr))
#define TOREPPTR(lst_ptr) ((list_rep *)(lst_ptr))
static status allocate_node (list_rep *p_lst, generic_ptr data)
{
list lst = (list) malloc(sizeof(node));
if (lst == NULL)
return ERROR;
*p_lst = lst;
DATA(lst) = data;
NEXT(lst) = NULL;
return OK;
}
static void free_node (list *p_lst)
{
free(*p_lst);
*p_lst = NULL;
extern status init_circ_list (list *p_lst)
{
*p_lst = NULL;
return OK;
}
extern bool empty_circ_list (list lst)
{
return (lst == NULL) ? TRUE : FALSE;
}
status circ_insert (list *p_lst, generic_ptr data)
{
list_rep lst, *p_lst_rep = TOREPPTR(p_lst);
if (allocate_node(&lst, data) == ERROR)
return ERROR;
if (empty_circ_list(*p_lst)) {
NEXT(lst) = lst;
*p_lst_rep = lst;
} else {
NEXT(lst) = NEXT(*p_lst_rep);
NEXT(*p_lst_rep) = lst;
}
return OK;
}
status circ_length (list lst)
{
list lastreturn;
int length;
length = 0;
lastreturn = NULL;
while((lastreturn = circ_list_iterator(lst, lastreturn)) != NULL)
length++;
return length;
}
status nth_node (list lst, int number)
{
list tmp;
if (empty_circ_list(lst) == TRUE)
return NULL;
if (number == -1)
return lst;
tmp = lst;
do {
tmp = NEXT(tmp);
number--;
} while (number > 0 && tmp != lst);
return (number != 0) ? NULL : tmp;
return 0;
}
status circ_traverse (list lst, status (*p_func_f)())
{
list tmp;
if (empty_circ_list(lst) == TRUE)
return OK;
tmp = lst;
do {
tmp = NEXT(tmp);
if ((*p_func_f)(DATA(tmp)) == ERROR)
return ERROR;
} while (tmp != lst) ;
return OK;
}
circular_list.h
#include "globals.h"
#ifndef _CIRCULAR_LIST_H
#define _CIRCULAR_LIST_H
typedef list lastreturn;
ABSTRACT_TYPE(list);
extern status init_circ_list (list *p_lst);
extern bool empty_circ_list (list lst);
extern status circ_insert (list *p_lst, generic_ptr data);
extern status circ_append (list *p_lst, generic_ptr data);
extern status circ_delete (list *p_lst, generic_ptr *p_data);
extern status circ_delete_node(list *p_lst, list node);
extern status circ_list_iterator (list lst, list lastreturn);
extern status circ_length (list lst);
extern status nth_node (list lst, int number);
extern status circ_traverse (list lst, status (*p_func_f)());
extern generic_ptr circ_head(list lst);
extern list circ_tail(list lst);
#endif
globals.h
#ifndef _GLOBALS_H
#define _GLOBALS_H
typedef enum {OK, ERROR} status;
typedef enum {FALSE = 0, TRUE = 1} bool;
typedef void *generic_ptr;
typedef unsigned char byte;
#define ABSTRACT_TYPE(t)
typedef void *(t)
#endif
在circ_length中,你應該返回一個狀態,但是你要返回一個int。 – Jason 2012-03-30 19:59:15
我在gcc下得到了很多編譯錯誤,但沒有那個消息。 – Taymon 2012-03-30 20:01:53
這些是即時通訊使用gcc -Wall -g -ansi -pedantic -c circular_list.c編譯我的代碼的唯一的錯誤 – Cka91405 2012-03-31 02:33:00