0

typedefcalledで作成した型へのポインターをパラメーターとして受け取る関数を作成しようとしていますNodeTypetypedef名前にリンケージがないことをぼんやりと理解しています。NodeType型の両方のインスタンスが同じ翻訳単位にあるように見えるときに、次のエラーが発生する理由がわかりません。

コードは次のとおりです。

#include <stdio.h>

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]

ご協力いただきありがとうございます!

4

2 に答える 2

0

あなたのprintList関数は の本体内で定義されておりmain、コンパイラを混乱させています。次のようprintListに本体の外に移動します。main

#include <stdio.h>

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(")");   
}

これを実行してコンパイルしたらprintList、 内からどのように、どこで呼び出すかを理解する必要がありますmain

于 2013-04-17T06:40:05.083 に答える
0

メイン関数内で関数を宣言することはできません。関数のプロトタイプと宣言をメイン ループの外に置きます。関数プロトタイプ ( void printList(NodeType *);) は、関数を実際に使用する前に宣言する必要があります。また、メインの外で関数の前に構造を宣言します。

typedefにも間違いがあります

       typedef struct NodeTag{
        char* Airport;
        NodeTag * Link; <-- missing struct prefix              
        } NodeType;
于 2013-04-17T06:51:39.753 に答える