我无法弄清楚C ++语法错误"期望`;'在'{'令牌"之前

时间:2014-05-21 19:29:24

标签: c++ syntax constructor

我有这个简单的代码,并且出现语法错误:

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

我把星星放在67和73的线条周围,这是该类的构造函数。我是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;
}

2 个答案:

答案 0 :(得分:6)

如果要在函数内定义类,则必须在类定义中内联定义该类的每个方法。例如:

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

答案 1 :(得分:4)

由于您不熟悉C ++,我猜测虽然另一个答案会让您的代码编译,但您可能想尝试更标准的方法来使用类头和实现文件,这会让您习惯于可重用的类而不是main()(或其他函数)中定义的类。

将类声明移动到名为&#34; Patient.h&#34;的文件中。并将实现(函数的定义)移动到&#34; Patient.cpp&#34;。在您的主文件和Patient.cpp中,包括Patient.h

这是一名患者:

#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中留下了:

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

如果您在IDE中工作,它将为您编译main.cpp和Patient.cpp;如果您在命令行中使用g ++或clang,请确保在编译时包含两个.cpp文件,例如

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

...然后你可以运行./myPatientProgram来查看你的程序运行。

HTH