C++ Cootie 游戏错误

标签 c++

我完成了大部分代码,我应该创建一个 cootie 游戏,根据他们 throw 的骰子为每个玩家 cootie 添加 ARM 腿头等。然而,当我尝试编译它时,我一直遇到错误,我不确定我应该做什么来消除它们。我非常不擅长编程,但我会尽力理解您的回答,非常感谢您的帮助!

错误如下:

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__) */

骰子.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?
}

骰子.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

播放器.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;
}

播放器.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

主要.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;
}

最佳答案

显然有很多地方需要解决。

第一个问题,

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

表示 cootie.h 应该包含在 Player.h 中,所以 class cootie 的声明在使用时是可见的。

关于C++ Cootie 游戏错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20509416/

相关文章:

c++ - 在类模板的其他未定义成员类的显式特化中定义隐藏 friend 是否合法?

c++ - 使用成员函数重载 bool 运算符

c++ - enable_if 中的短路运算符

c++ - 将 CStringW 转换为 CString 结果问号

带指针的映射的 C++ 输出

c++ - 为可变参数模板中给定的所有类型添加模板特化

c++ - 执行某些代码后可以显式调用构造函数吗?

c++ - 如何在JNI中将java异常作为jobobject抛出

C++ 模板元编程 : how to deduce type in expression pattern

c++ - MongoDB C++ Legacy 驱动程序 - 如何使用 UNIX 域套接字进行连接