関数 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: エラー: 不完全な型へのポインターを逆参照しています
注:マネージャーファイルを変更できません(演習に属しています)。メインのみを変更できます。
助けてくれてありがとう!