我正在試圖製作一個發生在醫院的小遊戲。下面的代碼:重新定義?
第一頭:
#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 ::出於某種原因,我知道我不應該在標題中使用名稱空間。
一個問題是你需要'#包括'在頭。 –
jogojapan
錯誤發生在哪裏,它讀的是什麼,以及你如何調用編譯器? – bitmask
錯誤發生在每個功能後...例如: bool Patient :: cure(){...}導致錯誤重新定義'cure' – IanPanz