-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

#include <string>Patient.h の一番上に追加してみてください。私がそれをしたとき、それは私のためにコンパイルされました。

于 2012-12-03T01:02:38.133 に答える
0

ただの予感ですが、ファイルの 1 つではPatient.cppなく を含めました。Patient.h

于 2012-12-03T01:07:17.830 に答える