このコードを正しく動作させることができません。コンパイルしようとすると、次の 3 つのいずれかが発生します。エラーは発生しませんが、プログラムを実行するとすぐにロックされます。または、正常にコンパイルされますが、「セグメンテーション違反」と表示され、実行すると終了します。または、コンパイル時に警告が表示されます。
"conflicting types for ‘addObjToTree’
previous implicit declaration of ‘addObjToTree’ was here"
しかし、「セグメンテーション違反」と表示され、実行しようとすると終了します。
gccを使用してMac OS X 10.6を使用しています。
ゲーム obj.h:
typedef struct itemPos {
float x;
float y;
} itemPos;
typedef struct gameObject {
itemPos loc;
int uid;
int kind;
int isEmpty;
...
} gameObject;
内部ルーチン.h:
void addObjToTree (gameObject *targetObj, gameObject *destTree[]) {
int i = 0;
int stop = 1;
while (stop) {
if ((*destTree[i]).isEmpty == 0)
i++;
else if ((*destTree[i]).isEmpty == 1)
stop = 0;
else
;/*ERROR*/
}
if (stop == 0) {
destTree[i] = targetObj;
}
else
{
;/*ERROR*/
}
}
/**/
void initFS_LA (gameObject *target, gameObject *tree[], itemPos destination) {
addObjToTree(target, tree);
(*target).uid = 12981;
(*target).kind = 101;
(*target).isEmpty = 0;
(*target).maxHealth = 100;
(*target).absMaxHealth = 200;
(*target).curHealth = 100;
(*target).skill = 1;
(*target).isSolid = 1;
(*target).factionID = 555;
(*target).loc.x = destination.x;
(*target).loc.y = destination.y;
}
main.c:
#include "game-obj.h"
#include "internal-routines.h"
#include <stdio.h>
int main()
{
gameObject abc;
gameObject jkl;
abc.kind = 101;
abc.uid = 1000;
itemPos aloc;
aloc.x = 10;
aloc.y = 15;
gameObject *masterTree[3];
masterTree[0] = &(abc);
initFS_LA(&jkl, masterTree, aloc);
printf("%d\n",jkl.factionID);
return 0;
}
うまくいかない理由がわかりません。addObjToTree(...)
gameObject 構造体へのポインターの配列である masterTree の次の空き領域に、gameObject へのポインターを追加したいだけです。addObjToTree(target, tree);
さらに奇妙なことに、行を削除するinitFS_LA(...)
と完全に機能します。で検索する関数を既に作成しておりmasterTree
、uid
新しいgameObject
ものをinitFS_LA(...)
(addObjToTree
行なしで)初期化しても正常に機能します。ヘッダーファイル内の関数を再配置し、それらを別のヘッダーファイルに入れ、プロトタイプを作成しようとしました、 の順序を並べ替え、#include
を使用する代わりにポインター変数を明示的に作成します&jkl
が、まったく機能しません。何か案は?どんな助けにも感謝します