クラス内のメンバに別のクラスオブジェクトのポインタを含めたい

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
bidou
記事: 6
登録日時: 9年前

クラス内のメンバに別のクラスオブジェクトのポインタを含めたい

#1

投稿記事 by bidou » 9年前

VisualStudio2013でゲーム制作用のC++プログラムをしています。
以下のようなクラスの構造を作ったのですが
件名とおりクラス内のメンバに別のクラスオブジェクトのポインタ(今回はShotクラスにActionクラスオブジェクトのポインタ)
を含めたいのですが、いくつかのコンパイルエラーがでてしまい困っています。
どのような点を改良すれば以上の構造を実現できるかが知りたいです。
よろしくお願いします。

Action.h

コード:

#pragma once
#include "Shot.h"


#define A_LIST_MAX 1024
class Action{
public:
	int id;
	int target;//アクションがくっついてるオブジェクトのarraynumと一致
	int type;//移動か弾幕かその他か
	int stage;//利用するステージ
	Action();
	~Action();
	void Process();
	//Enemy_Shot *A_p;
	//元Danmakuクラスの引数(後々Friendでアクセスさせる仕様に)
	double startangle;
	double speed;
	double anglespace;
	int way;
	int timespace;
	int endtime;

private:

	
};
extern Action ActionList[A_LIST_MAX];
//extern Action *action[1024];
Shot.h

コード:

#pragma once

#include"Action.h"
class Enemy_Shot{
public:
	Enemy_Shot();
	~Enemy_Shot();
	Action *action;//エラーがでる箇所
	int ActionID;
	Enemy_Shot *s;//自分自身へのポインタ(参照用)
	void Process();
	void Renew();//一部の値を元となるEnemyクラスと合わせる

	static int total;

	static int object_n;//
	double x;
	double y;
	int t;
	int id;
	
	int mode;//敵が死んだらShotが消えるか(0で消えない)
private:
	int exi;
	int actionflag;
};
extern Enemy_Shot * shot[1024];
エラー 2 error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません c:\users\Myname\documents\visual studio 2013\projects\fordebug\fordebug\shot.h 8 1 ForDebug
エラー 1 error C2143: 構文エラー : ';' が '*' の前にありません。 c:\users\Myname\documents\visual studio 2013\projects\fordebug\fordebug\shot.h 8 1 ForDebug


アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: クラス内のメンバに別のクラスオブジェクトのポインタを含めたい

#2

投稿記事 by みけCAT » 9年前

循環参照ですね。
Action.hではEnemy_Shotクラスや変数shotを使用していないので、無駄にShot.hをインクルードしてはいけません。

ちなみに、相互に依存する場合は、ヘッダをインクルードするかわりに前方宣言を使うといいでしょう。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

bidou
記事: 6
登録日時: 9年前

Re: クラス内のメンバに別のクラスオブジェクトのポインタを含めたい

#3

投稿記事 by bidou » 9年前

ありがとうございます、以下のように解決できました

コード:

#pragma once
class Action;

class Enemy_Shot{
public:
	Enemy_Shot();
	~Enemy_Shot();
	Action *action;//エラーがでる箇所
	int ActionID;
	Enemy_Shot *s;//自分自身へのポインタ(参照用)
	void Process();
	void Renew();//一部の値を元となるEnemyクラスと合わせる

	static int total;

	static int object_n;//
	double x;
	double y;
	int t;
	int id;
	
	int mode;//敵が死んだらShotが消えるか(0で消えない)
private:
	int exi;
	int actionflag;
};
extern Enemy_Shot * shot[1024];

コード:

#include"Shot.h"
#include"Action.h"
Enemy_Shot::Enemy_Shot(){
	action = new Action();
}
void Enemy_Shot::Process(){
	
}
void Enemy_Shot::Renew(){
	
}
Enemy_Shot *shot[1024];



閉鎖

“C言語何でも質問掲示板” へ戻る