排除编译时循环链表错误
好吧,我在使用 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);
}
Okay, so I'm getting these errors upon compile with 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
I've tried and tried, but to no avail to fix these errors. Thanks for any and all help.
Here's my .c file:
#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
And my .h file (note things are commented out because I tried things differently with no results):
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;
}*/
}
EDITED PROGRAM
Error:
file.c: In function âmainâ:
file.c:37: error: incompatible type for argument 2 of âinsertHeadCircularâ
The .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
}
The .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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
insertHeadCircular()
函数中,您将employeeList
的emp
成员视为employeeInfo *
。例如,您声明:但稍后执行此操作:
但是,您的
employeeList
类型包含employeeInfo
的实例,而不是指向该实例的指针:所以基本上您需要更正您的代码,以便您停止将结构分配给指针,而是将结构的地址分配给指针。
您的
printInOrder()
和printReverse()
函数很可能应采用employeeList *
参数,而不是employeeList
参数,还有...您应该检查它们使用的代码,以确保您不会在任何地方混淆两者。在头文件之外的其他位置定义函数也是一个好主意,例如在单独的
.c
源文件中。头文件应该只包含函数原型、宏和其他源文件可能需要的其他声明;您不需要其中的函数体,因为链接器可以在从其他源创建的目标文件中找到它们。当头文件被多个文件#include 时,像这样在头文件中定义函数会带来无尽的麻烦。更新后的代码中出现的错误
file.c:37: error: incomplete type for argument 2 of âinsertHeadCircularâ
指出您传递给insertHeadCircular() 的参数类型
不是您在声明中给出的类型——这是事实。您已声明并定义该函数以将employeeInfo
作为其第二个参数:...但在
main()
中,您改为向其传递一个指针:因此您需要改变其中之一。当您从 main 调用该函数时取消引用
insert
,或者更改insertHeadCircular()
以采用指针(并相应地更新主体)。后者可能更好,因为它避免了在调用函数时将整个结构复制到堆栈上。其他一些需要指出的事情:
您应该真正检查
main()
循环中scanf()
的返回结果。它会让您知道是否所有字段都已实际读取;现在,如果不是,您的程序将继续处理变量已经拥有的任何垃圾(可能像上一次迭代中读取的任何垃圾)。检查其他返回值(例如malloc()
的返回值)也是一个好主意,但在这种情况下scanf()
返回值尤其重要。您也不要在程序末尾释放
insert
;当你的程序退出时,操作系统(几乎肯定)会清理它,但是当你完成它时,最好自己清理它。不过,就您使用它的方式而言,您实际上并不需要动态分配它;而是需要动态分配它。您可以只声明一个employeeInfo
并获取其地址(不过,无论哪种方式都可以)。In your
insertHeadCircular()
function, you treat theemp
member ofemployeeList
as though it were anemployeeInfo *
. For example, you declare:but then later do this:
However, your
employeeList
type contains an instance ofemployeeInfo
, not a pointer to one:So basically you need to correct your code so that you stop assigning a structure to a pointer, and instead assign the address of the structure to the pointer.
Most likely your
printInOrder()
andprintReverse()
functions should takeemployeeList *
arguments, rather thanemployeeList
ones, as well... and you should check the code you use for them to make sure you don't confuse the two anywhere.It's also a good idea to define your functions somewhere other than the header file, such as in a separate
.c
source file. The header file should just contain function prototypes, macros, and other declarations that may be needed for other source files; you don't need the function bodies in there, since the linker can find them in the object files created from your other sources. Defining functions in header files like that will cause endless headaches when the header file is#include
ed by more than one file.The error you get with your updated code,
file.c:37: error: incompatible type for argument 2 of âinsertHeadCircularâ
is pointing out that the type of argument you're passing toinsertHeadCircular()
isn't the type you gave in its declaration -- which is true. You've declared and defined that function to take anemployeeInfo
as its second argument:...but in
main()
you pass it a pointer instead:So you need to change one or the other. Either dereference
insert
when you call the function from main, or changeinsertHeadCircular()
to take a pointer instead (and update the body accordingly). The latter is probably better, since it avoids copying the entire structure onto the stack when you call the function.Some other things to point out:
You should really check the return from
scanf()
in your loop inmain()
. It would let you know whether all the fields were actually read; right now, if they weren't, your program just proceeds with whatever junk the variables already had (like whatever was read on the previous iteration, possibly). Checking other return values (like the return frommalloc()
) is a good idea too, but in this case thescanf()
return is particularly important.You also don't free
insert
at the end of your program; the OS will (almost certainly) clean it up when your program exits, but it's good practice to do it yourself when you're done with it. The way you use it, though, you didn't really need to dynamically allocate it anyway; you could have just declared anemployeeInfo
and taken its address (works either way, though).