user1539097
user1539097

Reputation: 109

I can't figure out C++ syntax error "expected `;' before ‘{’ token"

I have this simple code and am getting a syntax error:

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

I've put stars around the lines that are 67 and 73, which are the constructors for the class. I'm very new to C++ and can't find the syntax issue. This is the first time I've made a constructor.

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

Upvotes: 3

Views: 3715

Answers (2)

David
David

Reputation: 28178

If you want to define a class inside a function you have to define every method of that class inline in the class definition. For example:

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

Upvotes: 6

Corbell
Corbell

Reputation: 1393

Since you are new to C++ I'm guessing that while the other answer will get your code to compile you may want to try the more standard way of using class header and implementation files, which will get you used to having reusable classes rather than classes defined within main() (or other functions).

Just move the class declaration into a file called "Patient.h" and move the implementation (definition of the functions) to "Patient.cpp". In both your main file and Patient.cpp, include Patient.h

Here's a 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;}

And what's left in 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;
}

If you are working in an IDE it will compile both main.cpp and Patient.cpp for you; if you're using g++ or clang at the command line just make sure to include both .cpp files when you compile, e.g.

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

...then you can run ./myPatientProgram to see your program run.

hth

Upvotes: 4

Related Questions