2014-05-21 4 views
3

" '{'토큰 앞에 나는이 간단한 코드를 가지고 구문 오류가 점점 오전 : 나는 클래스의 생성자입니다 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; 
} 
+1

클래스/함수를 다른 함수 안에 선언하거나 정의 할 수 없습니다. –

+1

함수 안에서 클래스를 선언 할 수 없습니다 :'int main() {class Patient ...'! –

+7

그건 사실이 아니야. – Flexo

답변

6

함수 내에 클래스를 정의하려면 클래스 정의의 해당 클래스의 모든 메소드를 정의해야합니다. 예를 들어 :

당신은 C를 처음부터
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() 
     { 
      name="string"; 
      height=0; 
      weight=0; 
     } 
}; 
4

++ 내가 추측하고있어 다른 대답은 당신이 클래스 헤더 및 구현 파일을 사용하는 더 표준적인 방법을 시도 할 수 있습니다 컴파일 코드를 얻을 것이다 동안하는 것 main() (또는 다른 함수) 내에 정의 된 클래스보다는 재사용 가능한 클래스를 갖는 데 익숙해 져야합니다.

클래스 선언을 "Patient.h"라는 파일로 이동하고 구현 (함수 정의)을 "Patient.cpp"로 옮깁니다.

#ifndef Patient_h 
#define Patient_h 

#include <string> 

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

#endif 

Patient.cpp :

#include "Patient.h" 

Patient::Patient() { 
    name="string"; 
    height=0; 
    weight=0; 
} 

Patient::Patient(std::string n) {name=n;height=0;weight=0;} 

그리고 무엇 MAIN.CPP에 남아있는 두 메인 파일과 Patient.cpp에서는 Patient.h을 여기

Patient.h

을 것 같습니다

#include <iostream> 

#include "Patient.h 

int main() { 

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

당신은 당신을 위해 모두 MAIN.CPP 및 Patient.cpp를 컴파일 할 것 IDE에서 작업하는 경우; 명령 줄에서 g ++ 또는 clang을 사용하는 경우 컴파일 할 때 두 .cpp 파일을 모두 포함해야합니다.

$ g++ main.cpp Patient.cpp -o myPatientProgram 

... ./myPatientProgram을 실행하면 프로그램이 실행됩니다.

hth

+0

이것은 좋은 제안이지만 실제로는 과제이고 내가 원하는 것을 바꾼다면 그들은 짜증이납니다. . 나는 헤더 파일을 사용하여 내 미래의 일부가 될 것입니다 알아요. – user1539097