構造体

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

構造体

#1

投稿記事 by Rittai_3D » 13年前

はじめまして。3Dと言います。今、自作のインベーダーゲームを作っているのですが、以下のエラーがでて困っています。
game.cpp

コード:

#include "DxLib.h"
#include "common.h"
#include "fps.h"
#include "game.h"
#include "struct.h"

player_t Player;

Player.x = 200;			//x座標
Player.y = 400;			//y座標
Player.shot_flag_key = 0;	//ショットは打たれない

Enemy_t Enemy;

Enemy.flag = 0;	//死んだら1
Enemy.x = 380;//座標
Enemy.y =  10;//座標


int stage = 0;//
void stage1(){
	//計算フェーズ
	if(Keyboard_Get(KEY_INPUT_Z)==1)player_t.shot_flag_key = 1;//ショットは出る
	tif(player_y.shot_flag_screen == 1)player_t.shot_flag_key = 0;//ショットは出ない3
	//自機の移動(計算フェーズ)
	if(Keyboard_Get(KEY_INPUT_RIGHT)==1)x+=6;		//もし、右が押されたら、6ずつ移動する
	if(Keyboard_Get(KEY_INPUT_LEFT) ==1)x-=6;		//同上
	if(Keyboard_Get(KEY_INPUT_LSHIFT)==1 && Keyboard_Get(KEY_INPUT_RIGHT)==1)x+=3;//低速移動
	if(Keyboard_Get(KEY_INPUT_LSHIFT)==1 && Keyboard_Get(KEY_INPUT_LEFT) ==1)x-=3;//低速移動

	//描写フェーズ
	DrawFormatString(player_t.x,player_t.y,GetColor(255,255,0),"自");
	if(player_t.shot_flag_key == 1)DrawFormatString(player_t.x,player_t.y,GetColor(255,255,255),"↑");//ショットを表示
	}
void stage2(){
	DrawString(0,0,"stage2",GetColor(255,255,255));
              clear();
             WaitKey();
            	}
void stage3(){
	DrawString(0,0,"stage3",GetColor(255,255,255));
              clear();
             WaitKey();
	}
void stage4(){
	DrawString(0,0,"stage4",GetColor(255,255,255));
              clear();
             WaitKey();
	}
void stage5(){
	DrawString(0,0,"stage5",GetColor(255,255,255));
              clear();
             WaitKey();
	}
void ending(){
	DrawString(0,0,"end",GetColor(255,255,255));
	}
void clear(){//クリアしたら
	int clear;
	DrawBox(30,20,400,450,GetColor(255,255,255),true);
	DrawFormatString(100,200,GetColor(255,255,255),"残機 : %d",player_t.zanki);
	DrawFormatString(100,250,GetColor(255,255,255),"Bomb : %d",player_t.bomb);
	DrawFormatString(100,400,GetColor(255,255,0)  ,"Push Z Key!");
	if(CheckHitKey(KEY_INPUT_Z)==1){//もし、zキーが押されたら
		stage++;					//stage に1を足す
		if(stage == 1)stage2();
		if(stage == 2)stage3();
		if(stage == 3)stage4();
		if(stage == 4)stage5();
		if(stage >  5)ending();
		}
	}
struct.h

コード:

typedef struct{
	int x, y;			//プレイヤーの座標
	int shot_flag_key;		//自機のshot_flag(キー入力)
	}player_t;

typedef struct{
	int flag;			//表示flag
	int bomb;			//bomb
	int zanki;			//残機
	int idou_houkou;	//移動方向
	double idou_ryou;	//移動量	
	} Enemy_t;
までがソースコードです。汚くてすいません^^;
いかがエラーです。

コード:

------ ビルド開始: プロジェクト: インベーダー, 構成: Debug Win32 ------
コンパイルしています...
game.cpp
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(9) : error C2143: 構文エラー : ';' が '.' の前にありません。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(9) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(9) : error C2371: 'Player' : 再定義されています。異なる基本型です。
        c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(7) : 'Player' の宣言を確認してください。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(10) : error C2143: 構文エラー : ';' が '.' の前にありません。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(10) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(10) : error C2371: 'Player' : 再定義されています。異なる基本型です。
        c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(7) : 'Player' の宣言を確認してください。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(11) : error C2143: 構文エラー : ';' が '.' の前にありません。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(11) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(11) : error C2371: 'Player' : 再定義されています。異なる基本型です。
        c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(7) : 'Player' の宣言を確認してください。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(17) : error C2143: 構文エラー : ';' が '.' の前にありません。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(17) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(17) : error C2371: 'Enemy' : 再定義されています。異なる基本型です。
        c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(15) : 'Enemy' の宣言を確認してください。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(18) : error C2143: 構文エラー : ';' が '.' の前にありません。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(18) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(18) : error C2371: 'Enemy' : 再定義されています。異なる基本型です。
        c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(15) : 'Enemy' の宣言を確認してください。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(19) : error C2143: 構文エラー : ';' が '.' の前にありません。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(19) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(19) : error C2371: 'Enemy' : 再定義されています。異なる基本型です。
        c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(15) : 'Enemy' の宣言を確認してください。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(25) : error C2143: 構文エラー : ';' が '.' の前にありません。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(25) : error C2143: 構文エラー : ';' が '.' の前にありません。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(26) : error C2065: 'player_y' : 定義されていない識別子です。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(26) : error C2228: '.shot_flag_screen' の左側はクラス、構造体、共用体でなければなりません
        型は ''unknown-type'' です。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(26) : error C2146: 構文エラー : ';' が、識別子 'player_t' の前に必要です。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(26) : error C3861: 'tif': 識別子が見つかりませんでした
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(26) : error C2275: 'player_t' : この型は演算子として使用できません
        c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\struct.h(5) : 'player_t' の宣言を確認してください。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(28) : error C2065: 'x' : 定義されていない識別子です。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(29) : error C2065: 'x' : 定義されていない識別子です。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(30) : error C2065: 'x' : 定義されていない識別子です。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(31) : error C2065: 'x' : 定義されていない識別子です。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(34) : error C2275: 'player_t' : この型は演算子として使用できません
        c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\struct.h(5) : 'player_t' の宣言を確認してください。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(34) : error C2275: 'player_t' : この型は演算子として使用できません
        c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\struct.h(5) : 'player_t' の宣言を確認してください。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(35) : error C2059: 構文エラー : '.'
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(50) : error C2039: 'zanki' : 'player_t' のメンバではありません。
        c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\struct.h(1) : 'player_t' の宣言を確認してください。
c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\game.cpp(51) : error C2039: 'bomb' : 'player_t' のメンバではありません。
        c:\documents and settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\struct.h(1) : 'player_t' の宣言を確認してください。
ビルドログは "file://c:\Documents and Settings\(ユーザー名)\デスクトップ\game_project\game\game_2\インベーダー\Debug\BuildLog.htm" に保存されました。
インベーダー - エラー 34、警告 0
========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ==========
いろいろと工夫しましたが、エラーが消えません。
環境は windows XP ,VC++2008 を使用しています。
C言語は初めて3ヶ月程度で、構造体とポインタが若干判らないです^^;
ライブラリはDXライブラリを使っています。
このエラーはどうしたら消えるでしょうか?お教え願います。
初心者です

box
記事: 2002
登録日時: 15年前

Re: 構造体

#2

投稿記事 by box » 13年前

3D_3D さんが書きました:

コード:

Player.x = 200;			//x座標
Player.y = 400;			//y座標
Player.shot_flag_key = 0;	//ショットは打たれない
Enemy.flag = 0;	//死んだら1
Enemy.x = 380;//座標
Enemy.y =  10;//座標
とりあえず、このあたりをどこかの関数の中に書いてみてはどうでしょうか。
バグのないプログラムはない。
プログラムは思ったとおりには動かない。書いたとおりに動く。

アバター
softya(ソフト屋)
副管理人
記事: 11677
登録日時: 15年前
住所: 東海地方
連絡を取る:

Re: 構造体

#3

投稿記事 by softya(ソフト屋) » 13年前

関数外で代入はできません。
関数外で出来るのは、変数の宣言・定義、マクロの定義、プロトタイプ宣言だけです。

【補足です】
それと謎変数の式もあるので色々直さないとダメですね。
enemyに
int bomb; //bomb
int zanki; //残機
があるのはどう見ても間違いです。
それと初期化しようとしてません。

インデントも見づらいです。

コード:

#include "DxLib.h"
#include "common.h"
#include "fps.h"
#include "game.h"
#include "struct.h"

player_t Player;

Player.x = 200;         //x座標
Player.y = 400;         //y座標
Player.shot_flag_key = 0;   //ショットは打たれない

Enemy_t Enemy;

Enemy.flag = 0; //死んだら1
Enemy.x = 380;//座標
Enemy.y =  10;//座標


int stage = 0;//
void stage1() {
	//計算フェーズ
	if( Keyboard_Get( KEY_INPUT_Z ) == 1 )player_t.shot_flag_key = 1; //ショットは出る
	tif( player_y.shot_flag_screen == 1 )player_t.shot_flag_key = 0; //ショットは出ない3
	//自機の移動(計算フェーズ)
	if( Keyboard_Get( KEY_INPUT_RIGHT ) == 1 )x += 6; //もし、右が押されたら、6ずつ移動する
	if( Keyboard_Get( KEY_INPUT_LEFT ) == 1 )x -= 6; //同上
	if( Keyboard_Get( KEY_INPUT_LSHIFT ) == 1 && Keyboard_Get( KEY_INPUT_RIGHT ) == 1 )x += 3; //低速移動
	if( Keyboard_Get( KEY_INPUT_LSHIFT ) == 1 && Keyboard_Get( KEY_INPUT_LEFT ) == 1 )x -= 3; //低速移動

	//描写フェーズ
	DrawFormatString( player_t.x, player_t.y, GetColor( 255, 255, 0 ), "自" );
	if( player_t.shot_flag_key == 1 )DrawFormatString( player_t.x, player_t.y, GetColor( 255, 255, 255 ), "↑" ); //ショットを表示
}
void stage2() {
	DrawString( 0, 0, "stage2", GetColor( 255, 255, 255 ) );
	clear();
	WaitKey();
}
void stage3() {
	DrawString( 0, 0, "stage3", GetColor( 255, 255, 255 ) );
	clear();
	WaitKey();
}
void stage4() {
	DrawString( 0, 0, "stage4", GetColor( 255, 255, 255 ) );
	clear();
	WaitKey();
}
void stage5() {
	DrawString( 0, 0, "stage5", GetColor( 255, 255, 255 ) );
	clear();
	WaitKey();
}
void ending() {
	DrawString( 0, 0, "end", GetColor( 255, 255, 255 ) );
}
void clear() { //クリアしたら
	int clear;
	DrawBox( 30, 20, 400, 450, GetColor( 255, 255, 255 ), true );
	DrawFormatString( 100, 200, GetColor( 255, 255, 255 ), "残機 : %d", player_t.zanki );
	DrawFormatString( 100, 250, GetColor( 255, 255, 255 ), "Bomb : %d", player_t.bomb );
	DrawFormatString( 100, 400, GetColor( 255, 255, 0 )  , "Push Z Key!" );
	if( CheckHitKey( KEY_INPUT_Z ) == 1 ) { //もし、zキーが押されたら
		stage++;                    //stage に1を足す
		if( stage == 1 )stage2();
		if( stage == 2 )stage3();
		if( stage == 3 )stage4();
		if( stage == 4 )stage5();
		if( stage >  5 )ending();
	}
}
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。

Rittai_3D
記事: 525
登録日時: 13年前

Re: 構造体

#4

投稿記事 by Rittai_3D » 13年前

>box様、softya様

返信ありがとうございます。

関数外では代入出来ないんですね…^^;

インデントはパソコンが使える時にしてみたいと思います。

int zanki;
int bomb;

はただ単に入力ミスです^^; 消しておきます。

とりあえず、初期化関数を作ってみたいと思います。

ありがとうございました。
最後に編集したユーザー Rittai_3D on 2012年12月24日(月) 16:59 [ 編集 1 回目 ]
初心者です

box
記事: 2002
登録日時: 15年前

Re: 構造体

#5

投稿記事 by box » 13年前

3D_3D さんが書きました: 構造体は関数外では代入出来ないんですね…^^;
「構造体は」の部分は取り払ってください。念のため。
バグのないプログラムはない。
プログラムは思ったとおりには動かない。書いたとおりに動く。

ISLe
記事: 2650
登録日時: 15年前
連絡を取る:

Re: 構造体

#6

投稿記事 by ISLe » 13年前

関数の外で代入はできないですが初期化はできます。

コード:

player_t Player = {
    200,         //x座標
    400,         //y座標
    0   //ショットは打たれない
};
ただしゲームオーバーで即終了するような実行形式でも無い限り、初期設定する関数を用意するほうが良いと思います。

Rittai_3D
記事: 525
登録日時: 13年前

Re: 構造体

#7

投稿記事 by Rittai_3D » 13年前

>box様、ISLe様
返信ありがとうございます。

>「構造体は」の部分は取り払ってください。念のため。
消しておきました。ありがとうございます。
ISLe さんが書きました:関数の外で代入はできないですが初期化はできます。

コード:

player_t Player = {
    200,         //x座標
    400,         //y座標
    0   //ショットは打たれない
};
ただしゲームオーバーで即終了するような実行形式でも無い限り、初期設定する関数を用意するほうが良いと思います。
初めて知りました。まだまだ勉強不足ですね^^;

皆様、本当にありがとうございました。
この冬休みで完成するようにがんばります。
初心者です

閉鎖

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