使用另一个类对象作为私有成员创建类对象的数组

时间:2015-11-18 02:54:13

标签: c++ arrays constructor queue

所以我正在尝试制作一系列房间,每个房间有两个队列。我有一个类,其中包含将在房间中的人员的信息,队列算法的类和房间的类。 我首先尝试的是将Room中的2个队列作为私有成员,然后使用房间的默认构造函数来创建房间。在每次使用队列函数时收到错误后我做了一些谷歌搜索,发现我需要将构造函数作为:

Rooms(Person pat):NormalList(pat){};

以使类对象成为私有成员。 Rooms类看起来像:

class Rooms
    {
    private:
    Queue<Person> NormalList;
    Queue<Person> EmergencyList;
    bool doctor;
    string docName;
    string specialty;


public:
    Rooms();
    Rooms(Person pat):NormalList(pat){};

    void addToNormal(Person);
    void addToEmergency(Person);

    bool checkDoctor();
    string getDocName() const;
    string getSpecialty();
    bool setDoctor(Person);

    void removeDoc();
    void removePat(Person);

    int getEmergLength() const;
    int getNormLength() const;

    bool isEmergEmpty() const;
    bool isNormEmpty() const;
};

现在的问题是我无法弄清楚如何使我的房间对象成为一个数组。我最初把它作为

Rooms room[100];

但现在需要创建为:

Rooms room(Default /*object of class Person*/);

我无法弄清楚如何使它成为一个数组。你能帮我们一把吗?我已经挣扎了很长一段时间,只剩下一天了

MCV示例: 主:

#include "Rooms.h"
int main()
{
    Person Default;
    //Rooms rooms[100](Default);
    Rooms rooms[100];

    rooms[0].addToEmergency(Default);

    return 0;
}

Queue.h:

#include <iostream>
#include <string>
using namespace std;

#ifndef Queue_h
#define Queue_h


class FullQueue
{};

class EmptyQueue
{};

template <class ItemType>
struct NodeType;

template <class ItemType>
class Queue
{
private:
    NodeType <ItemType>* front;
    NodeType <ItemType>* rear;
    int length;

public:
    Queue();
    Queue(ItemType);
    void Enqueue(ItemType);
    bool IsFull() const;
};
#endif /* Queue_h */

Queue.cpp:

#include "Queue.h"


template <class ItemType>
Queue <ItemType>::Queue()
{

    front = NULL;
    rear = NULL;
    length = 0;

}

template <class ItemType>
Queue <ItemType>::Queue(ItemType pat)
{
    front = pat;
}

template <class ItemType>
void Queue<ItemType>::Enqueue(ItemType item)
{
    if(IsFull())
        throw FullQueue();

    length++;

    NodeType <ItemType>* tmpNode;

    tmpNode = new NodeType <ItemType>;
    tmpNode -> info = item;
    tmpNode -> next = NULL;
    if (rear == NULL)
    {
        front = tmpNode;
    }
    else
    {
        rear -> next = tmpNode;
    }
    rear = tmpNode;
}

Rooms.h:

#ifndef Rooms_h
#define Rooms_h

#include "Queue.h"
#include "Person.h"


class Rooms
{
private:
    Queue<Person> NormalList;
    Queue<Person> EmergencyList;
    bool doctor;
    string docName;
    string specialty;


public:
    Rooms();
    Rooms(Person pat):NormalList(pat){};

    void addToNormal(Person);
    void addToEmergency(Person);

    bool checkDoctor();
    string getDocName() const;
    string getSpecialty();
    bool setDoctor(Person);

    void removeDoc();
    void removePat(Person);

    int getEmergLength() const;
    int getNormLength() const;

    bool isEmergEmpty() const;
    bool isNormEmpty() const;
};
#endif /* Rooms_h */

Rooms.cpp:

#include "Rooms.h"
Rooms::Rooms()
{
    doctor = false;
    docName = " ";
    specialty = " ";
}
void Rooms::addToNormal(Person pat)
{
    if(!NormalList.IsFull())
        NormalList.Enqueue(pat);
}

Person.h:

#ifndef Person_h
#define Person_h
#include <iostream>
#include <string>
using namespace std;

class Person
{
private:
    bool doctor;
    int roomNum;

    string specialty;
    string name;

    bool patient;
    bool emergency;
    int age;

public:
    Person();
};
#endif /* Person_h */

Person.cpp:

#include "Person.h"

Person::Person()
{
    doctor = false;
    roomNum = 0;

    specialty = "";
    name = "";

    patient = false;
    emergency = false;
    age = 0;
}

错误:

Undefined symbols for architecture x86_64:
  "Queue<Person>::Enqueue(Person)", referenced from:
      Rooms::addToNormal(Person) in Rooms.o
  "Queue<Person>::Queue()", referenced from:
      Rooms::Rooms() in Rooms.o
  "Rooms::addToEmergency(Person)", referenced from:
      _main in main.o
  "Queue<Person>::IsFull() const", referenced from:
      Rooms::addToNormal(Person) in Rooms.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

0 个答案:

没有答案