0

I made hospital class and patient class, so what I am trying to do is to make Patient[] patients < this. However, there is showing some error error: expected unqualified-id before '[' token I do not know what it is wrong with that. Thank you for your response.

class Hospital
{
    public:
        Hospital();
        void determinePatientType();


    protected:

    private:
        Patient[] patients;
        char * fileName;
        int patientCapacity;
        char * hospitalName;
        int totalPatients;
};
Erik A
  • 31,639
  • 12
  • 42
  • 67
성기덕
  • 43
  • 1
  • 7
  • 2
    Do some research about [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). And of course read [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) as that syntax is wrong. – Some programmer dude Mar 17 '18 at 14:33
  • You cannot create an array without a size. Try `std::vector` instead. – Bo Persson Mar 17 '18 at 14:33
  • Does the Hospital class include the patient header file? – Alan Mar 17 '18 at 14:33
  • what if I want to use dynamic allocation ?.. Where can I assign the size of array of that object? Thanks. – 성기덕 Mar 17 '18 at 14:35
  • @Alan Yes I included header file of patient. – 성기덕 Mar 17 '18 at 14:36
  • Havent actually built this, but I think replacing the array of patients with Vector patients; Should do it? Also include the vecotr type – Alan Mar 17 '18 at 14:38
  • The correct syntax would be `Patient patients[10];`, but better use `std::vector patients;` as @Someprogrammerdude already suggested. Same goes for `fileName` and `hospitalName`, work with [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) instead. – Olaf Dietsche Mar 17 '18 at 14:39

1 Answers1

-1

1- That is not how to declare an array:

    Patient[] patients;

You must make the subscript operator after the identifier not before it.

    Patient patients[size];

2- You have to specify array size at compile time so:

patient pat[size];

The size must be constant at compile time. If you want a dynamic size then consider using dynamic arrays. Also i recommend using class vector.

To use a dynamic array you should use pointesrs:

patient* patients;

And layer on you can allocate it:

patients = new patient[size];

Your class could look like:

nclude using namespace std;

class Hospital{
       public:
        Hospital(int); // constructor
        ~Hospital(); // destructor
        Hospital();
        void determinePatientType();

    private:
        Patient* patients;
        char * fileName;
        int patientCapacity;
        char * hospitalName;
        int totalPatients;
};

Hospital::Hospital(int size){
    patients = new Patient[size]; // allocating dynamic memory
    // some initialization and processing here

}

Hospital::~Hospital(){
    delete[] patients; //freeing up memory
}
Maestro
  • 2,512
  • 9
  • 24