プロが教える店舗&オフィスのセキュリティ対策術

今年の9月にC++を学校ではじめた初心者です。

書いているプログラムで、コンパイルしようとしたときに、)が必要
と出てくるのですが、対応している()はあるので、どうすればいいか困っています。
使っているコンパイラはBorland C++ です。

よろしくおねがいします。

class DraculaCastle
{
     VanHelsing vh;
     CountDraculia cd;
ここ→ CountDraculia cdstrong(400, rand()%10, 30, 0);
public:
     DraculaCastle();
     void Play();
};

A 回答 (6件)

#4の回答補足について


>「:cdstrong(400, rand()%10, 30, 0) // <= ココに追加」
>の部分なんですが、この書き方で二つかけますか?

初期化子は、2つ以上、かけます
複数のメンバー変数を書く場合は、「,」(カンマ)で区切ってください
書き方は以下のようになります

class DraculaCastle
{
    VanHelsing vh;
    CountDraculia cd;
    CountDraculia cdstrong;
public:
    DraculaCastle();
    void Play();
};

DraculaCastle::DraculaCastle()
:cd(100+rand()%10*10, rand()%10, rand()%5*5, 0, 20+rand()%2*10),
cdstrong(300, rand()%10, 30, 0, 30+rand()%3*10)
{
~以下略~

#5の回答補足について
> 上のコードを使ったらできました。ですが、なぜはじめの )が必要 というエラーがでたのでしょうか?

コンパイラがどのように構文解析をしていったかによりますので、なんともいえません
少なくとも、構文とあっていなかったと、いうことです。
たとえば、同じようなコードをVC++6でコンパイルすると、違うメッセージが表示されています。

あまり深く追求しなくてもよいかと思います
コンパイラの構成を勉強したいのであれば、別ですが...
    • good
    • 0

#4に追記ですが、クラス宣言が抜けていたので一応追加です


class DraculaCastle
{
     VanHelsing vh;
     CountDraculia cd;
     CountDraculia cdstrong;
public:
     DraculaCastle();
     void Play();
};

DraculaCastle::DraculaCastle()
:cdstrong(400, rand()%10, 30, 0) // <= ココに追加
{
    cout << "<you enter the dark castle of Count Draculia>" << endl;
    cout << "<everything around you is dusty>" << endl;
    cout << "<then, you sense a presence over you... looking down upon you>" << endl;
~中略~
    cout << "<your remaining MP>:" << vh.GetMP() << endl;
    cout << "<Count Draculia's remaining HP>:" << cd.GetHP() << endl;
}

この回答への補足

解決策、ありがとうございました。
上のコードを使ったらできました。ですが、なぜはじめの )が必要 というエラーがでたのでしょうか?

補足日時:2007/02/08 16:41
    • good
    • 0

このようにしては、どうですか?


DraculaCastle::DraculaCastle()
:cdstrong(400, rand()%10, 30, 0) // <= ココに追加
{
    cout << "<you enter the dark castle of Count Draculia>" << endl;
    cout << "<everything around you is dusty>" << endl;
    cout << "<then, you sense a presence over you... looking down upon you>" << endl;
~中略~
    cout << "<your remaining MP>:" << vh.GetMP() << endl;
    cout << "<Count Draculia's remaining HP>:" << cd.GetHP() << endl;
}

この回答への補足

「:cdstrong(400, rand()%10, 30, 0) // <= ココに追加」
の部分なんですが、この書き方で二つかけますか?

class DraculaCastle
{
    VanHelsing vh;
    CountDraculia cd;
    CountDraculia cdstrong;
public:
    DraculaCastle();
    void Play();
};

DraculaCastle::DraculaCastle()
:cd(100+rand()%10*10, rand()%10, rand()%5*5, 0, 20+rand()%2*10)
:cdstrong(300, rand()%10, 30, 0, 30+rand()%3*10)
{
    cout << "<you enter the dark castle of Count Draculia>" << endl;
    cout << "<everything around you is dusty>" << endl;
    cout << "<then, you sense a presence over you... looking down upon you>" << endl;
中略
    cout << "<your remaining HP>:" << vh.GetHP() << endl;
    cout << "<your remaining MP>:" << vh.GetMP() << endl;
    cout << "<Enemy's remaining HP>:" << cd.GetHP() << endl;
}

このようにcd(strong, weak 等)を何種類か書き、VanHelsingと戦えさせたいのですが。。エラーになります。

補足日時:2007/02/08 19:14
    • good
    • 0

基本的に #2 でいいと思うんだけど....


「ちょっと違いました」とは, 何がどのように「ちょっと違った」のでしょうか?

この回答への補足

#2は
public:
     DraculaCastle();
     void Play();
のDraculaCastle();
を定義しているんですよね?でもDraculaCastleは
DraculaCastle::DraculaCastle(){
    cout << "<you enter the dark castle of Count Draculia>" << endl;
    cout << "<everything around you is dusty>" << endl;
    cout << "<then, you sense a presence over you... looking down upon you>" << endl;
    cout << "<you look up>" << endl;
    cout << "<suddently, a dark shape falls off one of the main pillars supporting the battlements>" << endl;
    cout << "<it falls right behind you, but very silently>" << endl;
    cout << "<you notice it has a shape of a man, but has wings>" << endl;
    cout << "I AM COUNT DRACULIA, AND YOU SHALL BOW DOWN BEFORE ME!!" << endl;
    cout << "<the shape booms in a deafening voice>" << endl;
    cout << "<finally... you realize that the legends were true...>" << endl;
    cout << "I have come to destroy you, Count Draculia." << endl;
    cout << "So it shall be..." << endl;
    cout << "Ready..." << endl;
    cout << "Fight!! " << endl;
    cout << "<Weapons>" << endl;
    cout << "<1. Silver Bullet: a strong but narrow attack. only damages attack location.>" << endl;
    cout << "<2. Holy Flame Thrower: a not so strong but has a wide damage zone.>" << endl;
    cout << "<3. SIGHT: tells you where the enemy is.>" << endl;
    cout << "<4. POISON: poisons the enemy. enemy receives damage every turn.>" << endl;
    cout << "<your remaining HP>:" << vh.GetHP() << endl;
    cout << "<your remaining MP>:" << vh.GetMP() << endl;
    cout << "<Count Draculia's remaining HP>:" << cd.GetHP() << endl;
}
のように、すでに定義してあるのです。それで、定義したいのはCountDraculiaで、DraculaCastleではないのです。

補足日時:2007/02/07 22:25
    • good
    • 0

> ここ→ CountDraculia cdstrong(400, rand()%10, 30, 0);


指摘の部分は、初期化を行いたいということでしょうか?

初期化であれば、コンストラクタに初期化子を使って初期化する方法があります。
例えば、下記のように記述すればよいかと思います
class DraculaCastle
{
     VanHelsing vh;
     CountDraculia cd;
     CountDraculia cdstrong;
public:
     DraculaCastle();
     void Play();
};

DraculaCastle::DraculaCastle()
: cdstrong(400, rand()%10, 30, 0)
{
}

この回答への補足

class CountDraculia
{
    int HP, pos, defense, poisoned;
public:
    CountDraculia(int HP1, int pos1, int defense1, int poisoned1):HP(HP1), pos(pos1), defense(defense1), poisoned(poisoned1){}
    void Attacked(Assault n);
    int GetHP() const{ return HP;}
};

void CountDraculia::Attacked(Assault n){
 ~内容~
}


というのをさっきのやつの前に書いたのですが、上のをベースとして、敵を何種類か作れるようにしたいんです。
CountDraculiaオブジェクトをDraculaCastle(戦いの場と考える)クラス内でつくり、HP, pos, defense, poisoned などを初期化しようとしました。
Schwarz20さんの回答のを試しましたが、DraculaCastle(); はその後に定義されているので、ちょっと違いました。。

補足日時:2007/02/07 21:05
    • good
    • 0

宣言の中で関数は呼べないと思うが。

この回答への補足

すいません…宣言の中で関数を呼ぶ とはどういうことでしょうか?

補足日時:2007/02/07 22:29
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています