gpt4 book ai didi

c - 错误 : conflicting types for a function

转载 作者:太空宇宙 更新时间:2023-11-04 01:00:35 24 4
gpt4 key购买 nike

下面是insertItem()申报 list.h ,

void insertItem(List *, void *newItem);

下面是insertItem()申报 tree.h ,

void insertItem(Tree *, void *item);


下面是Tree抽象,重用 List抽象,通过说#include"list/list.h"

#ifndef TREE_H /* Header guard */
#define TREE_H

#include"list/list.h"

.....

void insertItem(Tree *, void *item);
....

#endif

关于编译:

gcc -Wall -g -I. -DARRAY -DMULTI_WALK ./list/*.c ./tree/*.c testTree.c -o testTree

错误如下:

./tree/tree.h:50:6: error: conflicting types for ‘insertItem’
void insertItem(Tree *, void *item);
^
In file included from ./tree/tree.h:8:0,
from ./tree/multiWalkImpl.c:2:
./list/list.h:35:7: note: previous declaration of ‘insertItem’ was here
void insertItem(List *, void *newItem);

问题的本质是什么?因为insertItem()tree.h & list.h有不同的参数类型。

如何解决这个问题?

最佳答案

由于 C 没有函数重载,因此通常可以通过添加描述不同参数的前缀来解决此类冲突。

例如List_insertItemTree_insertItem


请注意,在实际代码中,应在任何此类冲突出现之前添加此类命名空间前缀,因为无法事先知道用户代码使用的函数名称。这样就省去了在以后更改名称时破坏现有代码的麻烦。

关于c - 错误 : conflicting types for a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41323276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com