-2

我正在尝试制作一个发生在医院的小游戏。这是代码:

首先是标题:

#ifndef Hospital_Patient_h
#define Hospital_Patient_h

class Patient
{
public:
    Patient();
    bool cured();
    bool died();
    bool treatable();
    void treat();
    void untreated();
    char consonant();
    char vowel();
    std::string getName();


    int age;
    int health;
    std::string name;

    bool operator==(const Patient &other)const;


};
#endif

这是cpp:

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

char CONSONANTS[] = 
{'b','c','d','f','g','h','j','k','l','m','n','p','r','s','t','v','z', '\0'};
char VOWELS[] = 
{'a','e','i','o','u', '\0'};
//generates a patient with initial health, a name, and an age from 10-79
Patient::Patient()
{
    int a= rand() % 80 + 10;
    health= 50;
    name= getName();
    age= a;
}

bool Patient::cured() {
    return health >= 100;
}

bool Patient::died() {
    return health <= 0;
}
//treatable if not dead and not cured
bool Patient::treatable() {  
    return !died() && !cured();
}

void Patient::treat() {
    if(treatable()) health= min(health + 10, 100);
}

void Patient::untreated() {
    if(treatable()) health= max(health - 1, 0);
}   

char Patient::consonant() 
{
    int index = rand() % 17; //CONSONANTS.size();
    return CONSONANTS[index];//rand.nextInt(CONSONANTS.length)];
}

char Patient::vowel() 
{
    int index = rand() % 5; //VOWELS.size();
    return VOWELS[index];//rand.nextInt(VOWELS.length)];
}

//generate a random name
string Patient::getName(){
    string s;
    s+=toupper(consonant());
    s+= vowel();
    s+=consonant();
    s+=vowel();
    if (rand() % 3 == 0) {
        s+=consonant();
    }
    if (rand() % 3 == 0) {
        s+=consonant();
        s+=vowel();
    }
    s+=(' ');
    s+=toupper(consonant());
    s+=vowel();
    if (rand() % 3 == 0) {
        s+=consonant();
    }
    s+=consonant();
    s+=vowel();
    if (rand() % 3 == 0) {
        s+=consonant();
    }
    if (rand() % 3 == 0) {
        s+=consonant();
        s+=vowel();
    }
    return s;
}
//overload ==
bool Patient::operator==(const Patient &other)const{
    //compare each room's room number field
    return (this->age == other.age && this->name == other.name);
}

到目前为止,在每个函数之后,我都收到一个错误,说“函数名称”的重新定义

我也遇到了构造函数和为标题中定义的变量赋值的问题。由于某种原因,标头也不喜欢 std:: ,我知道我永远不应该在标头中使用命名空间。

4

2 回答 2

0

只是一种预感,您已经包含Patient.cpp而不是包含Patient.h在您的一个文件中。

于 2012-12-03T01:07:17.830 回答
0

尝试添加#include <string>到 Patient.h 的最顶部。当我这样做时,它为我编译。

于 2012-12-03T01:02:38.133 回答