简体   繁体   English

问题初始化结构数组

[英]Problem Initializing an Array Of Structs

I am trying to initialize the following array of the following struct, but my code isn't compiling. 我正在尝试初始化以下结构的以下数组,但我的代码未编译。 Can anybody help me out? 有人可以帮帮我吗?

The struct/array: 结构/数组:

struct DiningCarSeat {
    int status;
    int order;
    int waiterNum;
    Lock customerLock;
    Condition customer;

    DiningCarSeat(int seatNum) {
        char* tempLockName;
        sprintf(tempLockName, "diningCarSeatLock%d", seatNum);
        char* tempConditionName;
        sprintf(tempConditionName, "diningCarSeatCondition%d", seatNum);
        status = 0;
        order = 0;
        waiterNum = -1;
        customerLock = new Lock(tempLockName);
        customer = new Condition(tempConditionName);
    }
} diningCarSeat[DINING_CAR_CAPACITY];

The relevant errors: 相关错误:

../threads/threadtest.cc: In constructor `DiningCarSeat::DiningCarSeat(int)':
../threads/threadtest.cc:58: error: no matching function for call to `Lock::Lock()'
../threads/synch.h:66: note: candidates are: Lock::Lock(const Lock&)
../threads/synch.h:68: note:                 Lock::Lock(char*)
../threads/threadtest.cc:58: error: no matching function for call to `Condition::Condition()'
../threads/synch.h:119: note: candidates are: Condition::Condition(const Condition&)
../threads/synch.h:121: note:                 Condition::Condition(char*)
../threads/threadtest.cc:63: error: expected primary-expression before '.' token
../threads/threadtest.cc:64: error: expected primary-expression before '.' token
../threads/threadtest.cc: At global scope:
../threads/threadtest.cc:69: error: no matching function for call to `DiningCarSeat::DiningCarSeat()'
../threads/threadtest.cc:51: note: candidates are: DiningCarSeat::DiningCarSeat(const DiningCarSeat&)
../threads/threadtest.cc:58: note:                 DiningCarSeat::DiningCarSeat(int)

Thanks in advance! 提前致谢!

Condition and Lock are have no default constructors. ConditionLock没有默认构造函数。 You should use initialization list to construct them. 您应该使用初始化列表来构造它们。

I would change/add constructors for Condition and Lock so they could accept const char* and int . 我将更改/添加ConditionLock构造函数,以便它们可以接受const char*int Then DiningCarSeat will look like: 然后DiningCarSeat将如下所示:

DiningCarSeat(int seatNum) : 
  status(0), 
  order(0), 
  waiterNum(-1), 
  cutomerLock( "diningCarSeatLock", seatNum), 
  customer("diningCarSeatCondition", seatNum) 
{}

There are multiple issues here: 这里有多个问题:

These should both be pointers, since you are new ing them in your constructor: 这些都应该是指针,因为您是在构造函数中对其进行了new

Lock customerLock;
Condition customer;

You don't declare a type for seatNum : 您没有为seatNum声明类型:

DiningCarSeat(seatNum) {

You don't allocate memory for tempLockName or tempConditionName : 您不为tempLockNametempConditionName分配内存:

    char* tempLockName;
    sprintf(tempLockName, "diningCarSeatLock%d", seatNum);
    char* tempConditionName;
    sprintf(tempConditionName, "diningCarSeatCondition%d", seatNum);

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

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