简体   繁体   English

初始化类的聚合成员

[英]initializing aggregated members of a class

I want to be able to initialize Seat inside of my method. 我希望能够在我的方法中初始化Seat。 I think I need to overload my '=' operator, but i am not sure. 我想我需要重载我的'='运算符,但我不确定。

void Ticket::setSize(Ticket ticket[], int a) {
   ticket[ticketCount].groupSize = a;
   ticket[ticketCount].seatInfo = new Seat[ticket[ticketCount].groupSize];
}

This is my Ticket class below. 这是我下面的票务类。 I am calling setSize. 我正在打电话给setSize。 and passing variables to initialize seatInfo in regards to the Ticket.groupSize. 并传递变量以针对Ticket.groupSize初始化seatInfo。 below are my Ticket and seat class 以下是我的机票和座位类别

class Ticket {
    private:
        Seat * seatInfo;
    double totalPrice;
    string flightInfo;
    int ticketNumber;
    int groupSize;

    void countPrice();
    public:
        Ticket(Seat[], int, string);

    ~Ticket();
    void print();
    double getPrice();
    string toString();
    static int getTicketCount();
    static void setTicket(Ticket[], int);
    static void setSize(Ticket[], int);
    const Seat & setSeat(Seat * );
    Seat & operator = (Seat & );
}

#endif

Seat.h 座位

class Seat {
   private:
      int seatNumber;
   double price;
   const double BUSINESS_PRICE = 200.00;
   const double ECONOMIC_PRICE = 100.00;
   Person * reserver;

   bool isBusinessClass(int);

   public:
       // constructor
      Seat();
   Seat(int, int, Person * );
   Seat(int, double, Person * );
}

Errors I'm getting are no constructor that can do it. 我遇到的错误是没有构造函数可以做到。 and no '=' operator function. 并且没有'='运算符功能。 If you need more info please let me know 如果您需要更多信息,请告诉我

seatInfo is defined as a pointer of Seat. seatInfo被定义为Seat的指针。

Seat *seatInfo; 座位* seatInfo;

ticket[ticketCount].seatInfo can be a pointer to a Seat instance but not an array of Seat instances. ticket [ticketCount] .seatInfo可以是指向Seat实例的指针,但不能是Seat实例的数组。

What about letting seatInfo point to 1 instance of Seat to see if the errors are still there? 让seatInfo指向Seat的1个实例以查看错误是否仍然存在怎么办?

ticket[ticketCount].seatInfo = new Seat(); ticket [ticketCount] .seatInfo = new Seat();

Constuctors are called as new Class() , in your case new Seat() . 构造函数称为new Class() ,在您的情况下称为new Seat() So replace the [] after new Seat with (). 因此,用()替换new Seat之后的[]。 Also, you are trying to call the constructor Seat(int) , which is not declared, so either use one of the 3 you declared or declare it. 另外,您尝试调用未声明的构造函数Seat(int) ,因此请使用声明的3之一或声明它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM