1

I am trying to create a doubly linked list for a homework. I'm using the book mastering algorithms with C by Kyle Louden as a guideline. The book states that i need to initialize a doubly linked list before I use it which I'm trying to do but when I enter my code visual studios tells me "incomplete type not allowed for the following line "void dlist_init(Dlist *list, void (*destroy)(void *data)){}", it also states that Dlist is undefined but from what I understood that wasn't a variable to be defined. I am very new to this so i might be completely wrong. please let me know any problems you see. the full code is as follows :

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bool.h"
#include "dlinklist.h"
#include "DlistElmt.h"
#include "Dlist.h"
#include "dlistdata.h"

/**************************************************************************************************/

int main(int argc, char* argv[])
{
    FILE* ifp, *ofp;
    //char outputFilename[] = argv[2];
    int hour, min;
    int* list;
    float temp;

    if(argc != 3)    /* argc should be 3 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf("usage: %s filename", argv[0]);
    }
    else
    {
        // We assume argv[1] is a filename to open
        ifp = fopen(argv[1], "r");

        if(ifp == 0)
        {

            printf("Could not open file\n");
        }

        else
        {

            ofp = fopen(argv[2], "w");
        }
    }
}

void dlist_init(Dlist* list, void (*destroy)(void* data))
{

    list->size = 0;
    list->destroy = destroy;
    list->head = NULL;
    list->tail = NULL;

    return;

}

I also have the following headers:

dlinklist.h

/**************************************************************************************************/

#ifndef DLIST_H
#define DLIST_H

#include <stdlib.h>
#include "DlistElmt.h"
#include "Dlist.h"
#include "Dlistdata.h"


/**************************************************************************************************/

#endif

Dlist.h

/**************************************************************************************************/

#ifndef DLIST_H
#define DLIST_H

#include <stdlib.h>
#include "dlinklist.h"
#include "DlistElmt.h"
#include "Dlistdata.h"


/**************************************************************************************************/

typedef struct Dlist_ {

int size;

int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);

DlistElmt *head;
DlistElmt *tail;

} Dlist;


#endi

dlistdata.h

/**************************************************************************************************/

#ifndef DLISTDATA_H;
#define DLISTDATA_H;

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dlinklist.h"
#include "DlistElmt.h"
#include "Dlist.h"


/**************************************************************************************************/

void dlist_init(Dlist *list, void (*destroy)(void *data));

#endif

DlistElmt.h

/**************************************************************************************************/

#ifndef DLISTELMT_H
#define DLISTELMT_H

#include <stdlib.h>
#include "dlinklist.h"
#include "Dlist.h"
#include "Dlistdata.h"


/**************************************************************************************************/

typedef struct DlistElmt_ {

void *data;
struct DlistElmt_ *prev;
struct DlistElmt_ *next;

} DlistElmt;

/**************************************************************************************************/

#endif
4

1 回答 1

0

您的头文件中出现了递归混乱。

  1. 您的主文件只需要#include "dlinklist.h". 该标题包括其他标题。
  2. dlinklist.h包含错误的守卫名称。 DLINKLIST_H不是DLIST_H
  3. dlistelmt.h根本不需要包含行。
  4. dlist.h只需要#include "dlistelmt.h",因为它在其声明中使用它。
  5. dlistdata.h只需要 `#include "dlist.h" 因为它在其声明中使用它。

您发布的主文件缺少一些花括号和格式。我刚刚编辑了你的问题来解决它。

于 2012-10-07T02:01:34.407 回答