2013-12-11 55 views
0

我完成了大部分代碼,我應該創建一個cootie遊戲,根據他們擲出的擲骰子,向每個玩家cootie添加胳膊腿頭等。當我嘗試編譯它時,我一直在收到錯誤,但我不確定我應該怎樣去除它們。我在編程方面絕對糟糕,但我會盡我所能理解您的迴應,謝謝您的幫助!C++ Cootie遊戲錯誤

下面是錯誤:

In file included from main.cpp:8: 
Player.h:14: error: ‘cootie’ does not name a type 
Player.h:15: error: ‘Dice’ does not name a type 
In file included from Player.cpp:1: 
Player.h:14: error: ‘cootie’ does not name a type 
Player.h:15: error: ‘Dice’ does not name a type 
Player.cpp: In member function ‘void Player::takeTurn()’: 
Player.cpp:24: error: ‘dice’ was not declared in this scope 
Player.cpp:25: error: ‘getRoll’ was not declared in this scope 
Player.cpp:27: error: ‘monster’ was not declared in this scope 
Player.cpp:29: error: expected `}' before ‘else’ 
Player.cpp: At global scope: 
Player.cpp:29: error: expected unqualified-id before ‘else’ 
Player.cpp:31: error: expected constructor, destructor, or type conversion before ‘<<’ token 
Player.cpp:32: error: expected unqualified-id before ‘else’ 
Player.cpp:34: error: expected constructor, destructor, or type conversion before ‘<<’ token 
Player.cpp:35: error: expected unqualified-id before ‘else’ 
Player.cpp:37: error: expected constructor, destructor, or type conversion before ‘<<’ token 
Player.cpp:38: error: expected unqualified-id before ‘else’ 
Player.cpp:40: error: expected constructor, destructor, or type conversion before ‘<<’ token 
Player.cpp:41: error: expected unqualified-id before ‘else’ 
Player.cpp:43: error: expected constructor, destructor, or type conversion before ‘<<’ token 
Player.cpp:44: error: expected declaration before ‘}’ token 
Player.h:14: error: ‘cootie’ does not name a type 
Player.h:15: error: ‘Dice’ does not name a type 

這裏是我的代碼:

cootie.cpp:

#include "cootie.h" 
#include <iostream> 
#include <string> 
using namespace std; 

string name; 
int legs, heads, eyes, wings, bodies, antennas; 

//accessors 
string cootie::getName(){return name;} 
int cootie::getLegs(){return legs;} 
int cootie::getHeads(){return heads;} 
int cootie::getEyes(){return eyes;} 
int cootie::getWings(){return wings;} 
int cootie::getBodies(){return bodies;} 
int cootie::getAntennas(){return antennas;} 

//mutators 
void cootie::setName(string na){ 
    name = na; 
} 
void cootie::setLegs(int l){ 
    legs = l; 
} 
void cootie::setHeads(int h){ 
    heads = h; 
} 
void cootie::setEyes(int e){ 
    eyes = e; 
} 
void cootie::setWings(int w){ 
    wings = w; 
} 
void cootie::setBodies(int b){ 
    bodies = b; 
} 
void cootie::setAntennas(int a){ 
    antennas = a; 
} 

cootie.h:

#ifndef COOTIE_H 
#define COOTIE_H 

#include <iostream> 
#include <string> 

using namespace std; 

class cootie{ 
private: 
    //listing members? 
    string name; 
    int legs, heads, eyes, wings, bodies, antennas; 


public: 
    //constuctors 
    //default constructors 
    cootie(){ 
     name = "undefined"; 
     legs = 0; 
     heads = 0; 
     eyes = 0; 
     wings = 0; 
     bodies = 0; 
     antennas = 0; 
    } 
    //non-defaultconstructors 
    cootie(string na, int l, int h, int e, int w, int b, int a) { 
     name = na; 
     legs = l; 
     heads = h; 
     eyes = e; 
     wings = w; 
     bodies = b; 
     antennas = a; 
    } 

    string getName(); 
    int getLegs(); 
    int getHeads(); 
    int getEyes(); 
    int getWings(); 
    int getBodies(); 
    int getAntennas(); 

    void setName(string na); 
    void setLegs(int l); 
    void setHeads(int h); 
    void setEyes(int e); 
    void setWings(int w); 
    void setBodies(int b); 
    void setAntennas(int a); 
}; 

#endif /* defined(____cootie__) */ 

Dice.cpp :

#include "Dice.h" 

#include <cstdlib> 
#include <ctime> 
//#include "cootie.h" 
#include <iostream> 
//#include <string> 
using namespace std; 

Dice::Dice(int sides) : 
    sides(sides) 
{} 


float Dice::getRoll() { 
    return ((rand() % sides) + 1); 
} 

void Dice::randomize(){ 
    srand((unsigned)time(NULL)); // or is it null? 
} 

Dice.h:

#ifndef DICE_H 
#define DICE_H 


class Dice 
{ 
    public: 
     Dice(int sides); 
     float getRoll(); 

     static void randomize(); //called once 
    protected: 
    private: 
     int sides; 
}; 

#endif // DICE_H 

Player.cpp:

#include "Player.h" 
#include "cootie.cpp" 
#include "Dice.h" 
#include <iostream> 
#include <string> 
using namespace std; 
//Player::Player(string name) 
//{ 
    // name = n; 
    //ctor 
//} 

//accessors 
//string Player::getName(){ 
//return name; 
//} 
//mutators 
//void Player::setName(string n){ 
// name = n; 
// } 

void Player::takeTurn() 
{ 
    dice.randomize(); 
    float f = getRoll(); 
    if (f == 1) 
    monster.setLegs(monster.getLegs()+1); 
    cout << "add a leg to cootie"<< endl; 
    else if (f == 2) 
    monster.setHeads(monster.getHeads()+1); 
    cout << "add a head to cootie"<< endl; 
    else if (f == 3) 
    monster.setEyes(monster.getEyes()+1); 
    cout << "add an eye to cootie"<< endl; 
    else if (f == 4) 
    monster.setWings(monster.getWings()+1); 
    cout << "add a wing to cootie"<< endl; 
    else if (f == 5) 
    monster.setBodies(monster.getBodies()+1); 
    cout << "add a body to cootie"<< endl; 
    else if (f == 6) 
    monster.setAntennas(monster.getAntennas()+1); 
    cout << "add an antenna to cootie"<< endl; 
} 

Player.h:

#ifndef PLAYER_H 
#define PLAYER_H 


class Player 
{ 
    public: 
     Player(); 
     void takeTurn(); 
//  void getName(string na); 
//  void setName(); 
    protected: 
    private: 
     cootie monster; 
     Dice dice(6); 
//  string name; 
}; 

#endif // PLAYER_H 

main.cpp中:

#include "Player.h" 
#include "cootie.h" 
#include "Dice.h" 
#include <cstdlib> 
#include <ctime> 
#include <iostream> 
#include <string> 

using namespace std; 

int main(){ 
    // cootie c("R2D2", 1, 1,0,2,1,0); 
cootie c; 
int x; 


    //Dice d(6); 
    //cout << "regular dice" << d.getRoll() << endl; 
    //Dice d20(20); 
    // cout << "20 size dice" <<d20.getRoll() << endl; 
    //Dice percentile(100); 
    //cout << "Percentile size dice"<< percentile.getRoll() << endl; 
    Player playerArray [2];// 2 player game 
    while (x<3) 
{ 
    for (x=0;x<2;x++) 
    { 
     playerArray[x].takeTurn(); 
     cout <<"Player "<< x; 
    cout << "Cootie " << x << endl; 
    cout << c.getLegs() << " Leg(s)" << endl; 
    cout << c.getHeads() << " Head(s)" << endl; 
    cout << c.getEyes() << " Eye(s)" << endl; 
    cout << c.getWings() << " Wing(s)" << endl; 
    cout << c.getBodies() << " Bodie(s)" << endl; 
    cout << c.getAntennas() << " Antenna(s)" << endl; 

    } 
} 
cout << "im done here" <<endl; 

    return 0; 
} 
+0

夥計,沒有人會通過所有這些代碼。 – OldProgrammer

+0

@OldProgrammer雖然我懷疑它與代碼本身無關。 – 0x499602D2

+3

頭文件中的#include「cootie.cpp」和「using namespace std;'只會導致悲傷。 –

回答

3

顯然有很多事情要解決。

第一個問題,

Player.h:14: error: ‘cootie’ does not name a type

指示cootie.h應從Player.h被包括,以便在使用時的class cootie聲明是可見的。

1

從錯誤判斷,你忘了導入一些頭文件。

這似乎還有另一個錯誤後來在涉及else語句的道路上。我會說這可能是你犯的一個語法錯誤。你的編譯器應該讓你很容易找到這個。