重新定义?

问题描述:

我正在试图制作一个发生在医院的小游戏。下面的代码:重新定义?

第一头:

#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 ::出于某种原因,我知道我不应该在标题中使用名称空间。

+0

一个问题是你需要'#包括'在头。 – jogojapan

+0

错误发生在哪里,它读的是什么,以及你如何调用编译器? – bitmask

+0

错误发生在每个功能后...例如: bool Patient :: cure(){...}导致错误重新定义'cure' – IanPanz

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

+0

并且在添加'#include'之前,您是否重现了所声明的错误?它应该没有区别。 –

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