2012-12-03 5 views
-1

병원에서 발생하는 작은 게임을 만들려고합니다. 여기 코드는 다음과 같습니다재정의?

먼저 헤더 : 지금까지 내가 '함수 이름'

의 재정을 말하는 오류가 각 기능 후

#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); 
} 

: 여기

#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의

나는 또한 생성자와 헤더에 정의 된 변수에 값을 할당하는 데 문제가 있습니다. 헤더도 어떤 이유로 std ::를 좋아하지 않으며 헤더에 네임 스페이스를 사용해서는 안된다는 것을 알고 있습니다.

+0

하나의 문제는 헤더에'#include '이 필요하다는 것입니다. – jogojapan

+0

정확히 어디에서 오류가 발생합니까, 무엇을 읽고, 정확히 얼마나 컴파일러를 호출합니까? – bitmask

+0

각 함수 뒤에 오류가 발생합니다. 예 : bool Patient :: cured() {...} 오류가 발생합니다. '치유 됨'의 재정의 – IanPanz

답변

0

#include <string>을 Patient.h 맨 위에 추가하십시오. 내가 그것을했을 때 그것은 나를 위해 편집되었습니다.

+0

그리고 '# include'를 추가하기 전에 명시된 오류를 재현 했습니까? 차이는 없어야합니다. –

0

파일 하나에 Patient.h 대신 Patient.cpp을 포함 시켰습니다.