C语言链表容器firstItem函数编译报错问题求助
C语言链表容器firstItem函数编译错误解决
错误根源
你在测试代码里犯了基础错误:变量名与函数名完全重名。定义的node *firstItem;和你的firstItem()函数同名,编译器会把调用firstItem(container1)中的firstItem识别为指针变量而非函数,触发"called object type 'node *' is not a function or function pointer"类编译错误。
修正方案
将测试代码中的变量名修改为与函数名不同的标识符,比如first_node:
printf("\nTesting firstItem() on a non-empty container:"); node *first_node; first_node = firstItem(container1); numTestsCompleted++; if (first_node != NULL && strcmp(first_node->data, "item 1") == 0) { printf("\n\tSUCCESS! firstItem() returned the first item in the container.\n"); } else { printf("\n\tFailed. firstItem() did not return the first item in the container.\n"); numTestsFailed++; }
额外检查点
头文件函数声明:确保
container.h中正确声明firstItem函数,避免隐式声明错误:#ifndef CONTAINER_H #define CONTAINER_H typedef struct node { char* data; struct node *next; }node; typedef struct container { struct node *top; }container; // 必须添加该函数声明 node* firstItem(container* containerADT); #endif参数传递正确性:确认
container1是container*类型指针,若container1是结构体实例,需传递其地址&container1。Makefile编译规则:确保Makefile能正确编译所有源文件,示例规则:
target = main srcs = main.c container.c objs = $(srcs:.c=.o) headers = container.h $(target): $(objs) gcc -o $@ $^ %.o: %.c $(headers) gcc -c -o $@ $< clean: rm -f $(target) $(objs)
内容的提问来源于stack exchange,提问作者Ky James
相关产品推荐
相关产品推荐

