1

関数 getManager は Manager 構造体を作成し、それへのポインターをタイプ ManagerP から返します (この関数は問題なく動作します)。定義は次のとおりです。

typedef struct Manager
{
int ID;
char name[MAX_NAME_LENGTH];
int numberOfStudentsInSchool;
double paycheck;
double attract;
} Manager;  
typedef struct Manager *ManagerP;

//My little code (that does the problem) is this (it's inside main):  

int foundId;
ManagerP manToFind = getManager(1, "manager2", 200.0 , 1.0, 1000); //this works ok.
foundId = manToFind->ID; //Error : "dereferencing pointer to incomplete type"

問題を見つけるのを手伝ってくれませんか? このエラーの意味がわかりません。

ありがとう。


編集:

ありがとう、しかし私はちょうど問題に気づいた. これらの行は「Manager.c」内にあります。

typedef struct Manager
{
    int ID;
    char name[MAX_NAME_LENGTH];
    int numberOfStudentsInSchool;
    double paycheck;
    double attract;
} Manager;
typedef struct Manager *ManagerP;

私のメイン ファイルには、さらにいくつかの定義を含む "Manager.h" を含めます。確認したところ、2 つの typedef コード (上に記述) をメイン ファイルに移動すると、すべて正常に動作します。しかし、これらの型定義を「Manager.c」内に配置する必要があります (それでも、「不完全な型へのポインターを逆参照しています」というエラーが発生します。問題は何ですか??

編集#2:わかりました、3つのファイルを投稿しています。それらをコンパイルすると、エラーが発生します:「GenSalary.c:9:21:エラー:不完全な型へのポインターを逆参照しています」

これらはファイルです:

// * Manager.h * :

#ifndef MANAGER_H
#define MANAGER_H
#define MAX_NAME_LENGTH 30

typedef struct Manager *ManagerP;

ManagerP getManager(int ID, const char name[], double paycheck, 
                    double attract, int numberOfStudentsInSchool);

#endif

// * Manager.c * :

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "Manager.h"
#define MAX_PRINT_LENGTH 1000

typedef struct Manager
{
int ID;
char name[MAX_NAME_LENGTH];
int numberOfStudentsInSchool;
double paycheck;
double attract;
} Manager;  

ManagerP getManager(int ID, char const name[], double paycheck,
                    double attract, int numberOfStudentsInSchool)
{
    ManagerP retVal = (ManagerP) malloc(sizeof(struct Manager));
    if (retVal == NULL)
    {
        fprintf(stderr, "ERROR: Out of memory in Manager\n");   
        exit(1);
    }
    retVal->ID = ID;
    strcpy(retVal->name, name);
    retVal->paycheck = paycheck;
    retVal->attract = attract;
    retVal->numberOfStudentsInSchool = numberOfStudentsInSchool;
    return retVal;
}

// * GenSalary.c * :

#include <stdio.h>
#include <stdlib.h>
#include "Manager.h"

int main()
{
    int foundId;
    ManagerP manToFind = getManager(1, "manager2", 200.0 , 1.0, 1000); //this works ok.
    foundId = manToFind->ID; //Error : "dereferencing pointer to incomplete type"
    return 0;
}

gcc -Wall GenSalary.c Manager.c -o GenSalary を使用してコンパイルすると、次のようになります: GenSalary.c:9:21: エラー: 不完全な型へのポインターを逆参照しています

注:マネージャーファイルを変更できません(演習に属しています)。メインのみを変更できます。

助けてくれてありがとう!

4

5 に答える 5

1

以下のコードは で正常に動作しgcc -Wall -pedantic -o test test.cます。ただし、typedefs を使用してポインター型を非表示にすることで、読みやすさが向上するかどうかはわかりません。エラーは、コードのコンテキストのどこかから発生する必要があります。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Manager
{
    int ID;
    char name[42];
    int numberOfStudentsInSchool;
    double paycheck;
    double attract;
} Manager;
typedef struct Manager *ManagerP;

ManagerP getManager(int x, char *y, double z, double q, int p)
{
    ManagerP foo = malloc(sizeof(Manager));
    foo->ID = x;
    strncpy(foo->name, y, 42);
    foo->numberOfStudentsInSchool = p;
    foo->paycheck = z;
    foo->attract = q;

    return foo;
}

int main(void)
{
    int foundId;
    ManagerP manToFind = getManager(1, "manager2", 200.0 , 1.0, 1000);
    foundId = manToFind->ID;
    printf("Found ID: %d\n", foundId);

    return 0;
}
于 2013-08-06T11:15:05.917 に答える