没有用于通过链接声明函数的链接的类型
我正在尝试编写一个函数,该函数将指向我使用typedef
创建的类型(称为NodeType
的指针作为参数。 我模糊地知道typedef
名称没有链接。 当NodeType
类型的两个实例看起来都在同一个翻译单元中时,我不确定为什么会出现以下错误。
这是守则:
#include int main(){ typedef struct NodeTag{ char* Airport; NodeTag * Link; } NodeType; //Declare print function void printList(NodeType *); void printList(NodeType * L){ //set N to point to the first element of L NodeType * N = L; //if the list is empty we want it to print () printf("( "); //while we are not at the Link member of the last NodeType while(N != NULL){ //get the Airport value printed printf("%s", N->Airport); //advance N N= N->Link; if(N != NULL){ printf(", "); } else{ //do nothing } } printf(")"); } return 0; }
这是我遇到的错误:
linkedlists.c: In function 'int main()': linkedlists.c: error: type 'NodeType {aka main()::NodeTag} with no linkage used to declare function 'void printList(NodeType*) with linkage [-fpermissive]
谢谢你的帮助!
你不能在main函数中声明你的函数。 将函数原型和声明放在主循环之外。 应该在实际使用函数之前声明函数原型( void printList(NodeType *);
)。 同时在main之外和函数之前声明你的结构。
你的typedef也有错误
typedef struct NodeTag{ char* Airport; NodeTag * Link; <-- missing struct prefix } NodeType;
你的printList
函数是在main
的主体中定义的,这使编译器感到困惑。 将printList
移动到main
之外,如下所示:
#include typedef struct NodeTag{ char* Airport; NodeTag * Link; } NodeType; //Declare print function void printList(NodeType *); int main(){ return 0; } void printList(NodeType * L){ //set N to point to the first element of L NodeType * N = L; //if the list is empty we want it to print () printf("( "); //while we are not at the Link member of the last NodeType while(N != NULL){ //get the Airport value printed printf("%s", N->Airport); //advance N N= N->Link; if(N != NULL){ printf(", "); } else{ //do nothing } } printf(")"); }
在完成此操作并将其编译后,您需要确定如何以及在何处从main
调用printList
。