" '{'토큰 앞에 나는이 간단한 코드를 가지고 구문 오류가 점점 오전 : 나는 클래스의 생성자입니다 67 및 73 인 선, 별 주위를 넣었습니다C++ 구문 오류 "expected";
file.cc:67: error: expected `;' before ‘{’ token
file.cc:73: error: expected primary-expression before ‘(’ token
file.cc:73: error: expected primary-expression before ‘n’
file.cc:73: error: expected `;' before ‘{’ token
. 나는 C++에 익숙하지 않고 구문 문제를 찾을 수 없다. 생성자를 만든 것은 이번이 처음입니다.
int main() {
class Patient {
private: // default is private but stating explicitly here for learning purposes
std::string name; //object
int height; //object
int weight; //object
public:
Patient(); //constructor
Patient(std::string); //constructor
void set_name (std::string n) {name=n;} //fn
void set_height (int h) {if (height>0){height=h;} else {height=0;}} //fn
void set_weight (int w) {if (weight>0){weight=w;} else {weight=0;}} //fn
std::string get_name(){return name;} //fn
int get_height(){return height;} //fn
int get_weight(){return weight;} //fn
int bmi(void) {
if ((height!=0) && (weight!=0))
return (weight/(height*height));
else
return 0;
}
};
Patient::Patient() { // **LINE 67**
name="string";
height=0,
weight=0;
}
Patient::Patient(std::string n) {name=n;height=0;weight=0;} // **LINE 73**
Patient father("Andrew Nonymous");
Patient mother("Ursula N. Known");
Patient baby;
//Father's height and weight are unknown.
mother.set_name("Ursula N. Nonymous");
mother.set_height(1.65);
mother.set_weight(58);
baby.set_height(0.495);
baby.set_weight(3.4);
std::cout << "Baby: " << baby.get_name() << " BMI: " << baby.bmi() << std::endl;
std::cout << "Mother: " << mother.get_name() << " BMI: " << mother.bmi() << std::endl;
std::cout << "Father: " << father.get_name() << " BMI: " << father.bmi() << std::endl;
return 0;
}
클래스/함수를 다른 함수 안에 선언하거나 정의 할 수 없습니다. –
함수 안에서 클래스를 선언 할 수 없습니다 :'int main() {class Patient ...'! –
그건 사실이 아니야. – Flexo