编译时循环链表错误疑难解答

时间:2011-10-10 15:26:45

标签: c compiler-errors linked-list

好的,所以我在使用gcc编译时遇到这些错误:

prelab6.h: In function âinsertHeadCircularâ:
prelab6.h:45: error: incompatible types in assignment
prelab6.h:46: error: incompatible types in assignment
prelab6.c: At top level:
prelab6.c:41: warning: data definition has no type or storage class
prelab6.c:41: warning: parameter names (without types) in function declaration
prelab6.c:41: error: conflicting types for âprintInOrderâ
prelab6.h:81: error: previous definition of âprintInOrderâ was here
prelab6.c:42: warning: data definition has no type or storage class
prelab6.c:42: warning: parameter names (without types) in function declaration
prelab6.c:42: error: conflicting types for âprintReverseâ
prelab6.h:112: error: previous definition of âprintReverseâ was here

我已经尝试过,但无法解决这些错误。感谢您的帮助。

这是我的.c文件:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "my.h"

int main ( int argc, char **argv )
{

char firstname[100];
char lastname[100];
int monthsEmployed;

FILE *fptr;
fptr = fopen(argv[1], "r");

if (fptr == NULL)
    printf ("Incorrect file reading!");

if (argc != 2)
    printf ("Incorrect number of arguments!");


employeeInfo *insert;
insert = malloc(sizeof(employeeInfo));
employeeList *head;
head = NULL;


while(!feof(fptr))
{   
    fscanf (fptr, "%100s %100s %d", firstname, lastname, &monthsEmployed);

    strcpy(insert->firstname, firstname);
    strcpy(insert->lastname, lastname); 
    insert->monthsEmployed = monthsEmployed;

    head = insertHeadCircular(head, insert);
}
}

printInOrder(head); // display the linked list
printReverse(head); // display the linked list in reverse

我的.h文件(请注意事情已被注释掉,因为我尝试的方式不同而没有结果):

typedef struct employeeInfo{
        char firstname[100];
        char lastname[100];
        int monthsEmployed;
}employeeInfo;

//Struct containing pointers to the next and previous used to make a circular linked list 
typedef struct list{
                employeeInfo emp;
                struct list *next;
                struct list *previous;
}employeeList;

employeeList *insertHeadCircular(employeeList *head, employeeInfo *emp);
void printInOrder(employeeList head);
void printReverse(employeeList head);

employeeList *insertHeadCircular(employeeList *head, employeeInfo *emp)
{
    employeeList *theprevious = head;
    employeeList *current;
    employeeList *thenext = head;
    current = malloc(sizeof(employeeList));
    employeeInfo *employee;

    if(thenext==NULL)
    {
        current->next = current;
        current->previous = current;
    }

    else
    {
        current->next = thenext;
        thenext->previous = current;

        while(theprevious->next != thenext)
        {
            theprevious = theprevious->next;
        }
        current->previous = theprevious;
        theprevious->next = current;
    }

    current->emp = (employeeInfo *)malloc(sizeof(employeeInfo));
    employee = current->emp;
    employee = malloc(sizeof(employeeInfo));
    strcpy(employee->firstname, emp->firstname);
    strcpy(employee->lastname, emp->lastname);
    employee->monthsEmployed = emp->monthsEmployed;

    /*
    employeeList *newcell, *first = head;

    if(head == NULL)
    {
        newcell = (struct list *)malloc(sizeof(struct list));
        strcpy(newcell->firstname, emp->firstname);
        strcpy(newcell->lastname, emp->lastname);
        newcell->monthsEmployed = emp->monthsEmployed;
        return newcell;
    }

    while(head->next != first)
    {
        head = head->next;
    }

    newcell = (struct list *)malloc(sizeof(struct list));
    head->next = newcell;
    strcpy(newcell->firstname, emp->firstname);
    strcpy(newcell->lastname, emp->lastname);
    newcell->monthsEmployed = emp->monthsEmployed;
    newcell->next = first;
    */
return current;
}


void printInOrder(employeeList head)
{
    /*employeeInfo *first = head;

    if (head == NULL)
        {
        printf("The circularly linked list is empty!\n");
        return;
        }

        do
        {

        printf("%s %s %d\n", emp.firstname, emp.lastname, head.monthsEmployed);

        head = head->next;
        } while(head != first);
*/
    /*employeeInfo current = head;
    employeeInfo start = head;
    int loop = 0;
    printf("--------------\n");
    while(current != start || loop==0)
    {
    loop++;
    printf("Employee: %s %s\nMonths Employed: %d", current->firstname, current->lastname, current->monthsEmployed);
    printf("--------------\n");
    current=current->next;
    }*/
}

void printReverse(employeeList head)
{/*
    employeeList current = head
    employeeInfo start = head
    int theloop=0;
    printf("--------------\n");
    while(current! = start || loop==0)
    {
    loop++;
    printf("Employee: %s %s\nMonths Employed: %d", current->firstname, current->lastname, current->monthsEmployed);
    printf("--------------\n");
    current=current->previous;
    }*/
}

已编辑的程序

错误:

file.c: In function âmainâ:
file.c:37: error: incompatible type for argument 2 of âinsertHeadCircularâ

.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "file.h"

int main ( int argc, char **argv )
{

char firstname[100];
char lastname[100];
int monthsEmployed;

FILE *fptr;
fptr = fopen(argv[1], "r");

if (fptr == NULL)
    printf ("Incorrect file reading!");

if (argc != 2)
    printf ("Incorrect number of arguments!");


employeeInfo *insert;
insert = malloc(sizeof(employeeInfo));
employeeList *head;
head = NULL;


while(!feof(fptr))
{   
    fscanf (fptr, "%100s %100s %d", firstname, lastname, &monthsEmployed);

    strcpy(insert->firstname, firstname);
    strcpy(insert->lastname, lastname); 
    insert->monthsEmployed = monthsEmployed;

    head = insertHeadCircular(head, insert);
}

printInOrder(head); // display the linked list
printReverse(head); // display the linked list in reverse
}

.h:

typedef struct employeeInfo{
    char firstname[100];
    char lastname[100];
    int monthsEmployed;
}employeeInfo;

typedef struct list{
    employeeInfo emp;
    struct list *next;
    struct list *previous;
}employeeList;
    typedef employeeList *listnode;

employeeList *insertHeadCircular(employeeList *head, employeeInfo emp);
void printInOrder(employeeList *head);
void printReverse(employeeList *head);



employeeList *insertHeadCircular(employeeList *head, employeeInfo emp)
{
    listnode newPtr;
    listnode firstPtr;
    listnode tempPtr;

    newPtr = (employeeList *)malloc(sizeof(employeeList));

    strcpy(newPtr->emp.firstname, emp.firstname);
    strcpy(newPtr->emp.lastname, emp.lastname);
    newPtr->emp.monthsEmployed = emp.monthsEmployed;

    if(head == NULL)
    {   
        newPtr->next = newPtr;
        newPtr->previous = newPtr;
        head = newPtr;
        firstPtr = newPtr;

    }
    else
    {
        tempPtr = firstPtr;
        newPtr->next = tempPtr;
        tempPtr->previous = newPtr;

        newPtr->previous = head;
        head->next = newPtr;
        firstPtr = newPtr;
    }
    return head;
}
void printInOrder(employeeList *head)
{
        listnode currentPtr = head;
        do
    {
            printf("%s %s %d\n",currentPtr->emp.firstname, currentPtr->emp.lastname, currentPtr->emp.monthsEmployed);
        currentPtr= currentPtr->previous;
    }
    while(currentPtr !=head);
}
void printReverse(employeeList *head)
{
        listnode currentPtr = head->next;
        do        
    {
        printf("%s %s %d\n",currentPtr->emp.firstname, currentPtr->emp.lastname, currentPtr->emp.monthsEmployed);
        currentPtr = currentPtr->next;
    }
    while(currentPtr != head->next);        
}

1 个答案:

答案 0 :(得分:1)

insertHeadCircular()功能中,您将emp的{​​{1}}成员视为employeeList。例如,您声明:

employeeInfo *

但稍后会这样做:

employeeInfo *employee;

但是,您的employee = current->emp; 类型包含employeeList的实例,而不是指向一个的实例:

employeeInfo

所以基本上你需要纠正你的代码,以便停止为指针分配结构,而是将结构的地址<_ em>分配给指针。

您的typedef struct employeeInfo{ char firstname[100]; char lastname[100]; int monthsEmployed; }employeeInfo; /* ... */ typedef struct list{ employeeInfo emp; /* see? Not a pointer. */ struct list *next; struct list *previous; }employeeList; printInOrder()函数最有可能采用printReverse()个参数,而不是employeeList *个参数...并且您应该检查您用于的代码他们确保你不要把两者混淆在一起。

在头文件以外的位置定义函数也是一个好主意,例如在单独的employeeList源文件中。头文件应该只包含其他源文件可能需要的函数原型,宏和其他声明;你不需要那里的函数体,因为链接器可以在从其他源创建的目标文件中找到它们。当头文件由多个文件.c编辑时,在头文件中定义函数会导致无穷无尽的麻烦。


您使用更新的代码#include得到的错误指出您传递给file.c:37: error: incompatible type for argument 2 of âinsertHeadCircularâ的参数类型不是您在声明中提供的类型 - 这是真的。您已声明并定义了该函数,以insertHeadCircular()作为其第二个参数:

employeeInfo

...但是在employeeList *insertHeadCircular(employeeList *head, employeeInfo emp) 中你传给了一个指针:

main()

所以你需要改变一个或另一个。从main调用函数时取消引用employeeInfo *insert; ... head = insertHeadCircular(head, insert); ,或者更改insert以取代指针(并相应地更新主体)。后者可能更好,因为它可以避免在调用函数时将整个结构复制到堆栈中。

要指出的其他一些事情:

您应该在insertHeadCircular()的循环中检查scanf()的回复。它会让你知道是否所有字段都被读取;现在,如果它们不是,你的程序只会继续变量已经存在的任何垃圾(就像上一次迭代中读取的那些,可能)。检查其他返回值(例如从main()返回)也是一个好主意,但在这种情况下,malloc()返回特别重要。

您在节目结束时也不会免费scanf();当你的程序退出时,操作系统(几乎可以肯定)会清理它,但是当你完成它时,最好自己动手。但是,你使用它的方式,你真的不需要动态分配它;你可以刚刚宣布一个insert并取其地址(但无论如何都是这样)。