简体   繁体   中英

How to initialize array of objects in constructor?

I have class called HighWaterDetector:

class HighWaterDetector {
public:
    HighWaterDetector(Device* device);
    NCD2Relay ncd2Relay;
    // Output output1;
    Output outputs[2];
    CloudMsgParser cloudMsgParser;
    Device * devicePtr;
};

How do I initialize the array of "Output" objects in the constructor of HighWaterDetector?

Output class:

 class Output
{
public:
    Output(ushort relayNum, NCD2Relay* ncd2RelayPtr);
    ushort relayNum;
    OutputStatus outputStatus;
    int setOutputOn(void);
    int setOutputOff(void);
    void process(void);
    NCD2Relay* ncd2RelayPtr;
};

with output constructor looking like:

Output::Output(ushort relayNum, NCD2Relay* ncd2RelayPtr2) {
    this->relayNum = relayNum;
    this->ncd2RelayPtr = ncd2RelayPtr2;
}

I am new to C++ and not sure if I can make the HighWaterDetector Constructor look like:

HighWaterDetector::HighWaterDetector(Device* device){
    ncd2Relay = NCD2Relay();
    outputs[0] = Output(1, &ncd2Relay);
    outputs[1] = Output(2, &ncd2Relay);
    cloudMsgParser = CloudMsgParser();

}

Getting compile errors:

highWaterDetector.cpp: In constructor 'HighWaterDetector::HighWaterDetector(Device*)':
highWaterDetector.cpp:8:52: error: no matching function for call to 'Output::Output()'
 HighWaterDetector::HighWaterDetector(Device* device){
                                                    ^
highWaterDetector.cpp:8:52: note: candidates are:
In file included from highWaterDetector.h:10:0,
                 from highWaterDetector.cpp:1:
output.h:20:2: note: Output::Output(ushort, NCD2Relay*)
  Output(ushort relayNum, NCD2Relay* ncd2RelayPtr);
  ^
output.h:20:2: note:   candidate expects 2 arguments, 0 provided
output.h:17:7: note: constexpr Output::Output(const Output&)
 class Output
       ^
output.h:17:7: note:   candidate expects 1 argument, 0 provided
output.h:17:7: note: constexpr Output::Output(Output&&)
output.h:17:7: note:   candidate expects 1 argument, 0 provided
highWaterDetector.cpp: In constructor 'HighWaterDetector::HighWaterDetector(Device*)':
highWaterDetector.cpp:8:52: error: no matching function for call to 'Output::Output()'
 HighWaterDetector::HighWaterDetector(Device* device){

Firstly, I think you need to have a default constructor for Output .

The default constructor is a constructor that takes no arguments. This is what will get called for each item in the array.

Secondly, please post your constructor for HighWaterDecorator . I suspect you could initialize ncdRelay in the constructor, and pass it to construct the two Output objects in the array.

Your Output class does not have a default (no input) constructor, only a non-trivial constructor that requires input values, so you cannot declare a fixed array of Output elements.

Give Output a default constructor:

class Output
{
public:
    Output(); // <-- here
    Output(ushort relayNum, NCD2Relay* ncd2RelayPtr);
    ...
};

Output::Output() {
    this->relayNum = 0;
    this->ncd2RelayPtr = 0;
}

Alternatively, you can give your existing constructor default parameter values so it can also act as a default constructor:

class Output
{
public:
    Output(ushort relayNum = 0, NCD2Relay* ncd2RelayPtr = 0);
    ...
};

Either way, you can then do this in the HighWaterDetector constructor:

HighWaterDetector::HighWaterDetector(Device* device)
    : devicePtr(device)
{
    outputs[0] = Output(1, &ncd2Relay);
    outputs[1] = Output(2, &ncd2Relay);
}

If you don't like that, then change your outputs[] array into a std::vector instead:

#include <vector>

class HighWaterDetector {
public:
    ...
    std::vector<Output> outputs;
    ...
};

HighWaterDetector::HighWaterDetector(Device* device)
{
    ...
    outputs.reserve(2);
    outputs.push_back(Output(1, &ncd2Relay));
    outputs.push_back(Output(2, &ncd2Relay));
    ...
}

If you are using C++11 or above, You should write your code like this:

class Output
{
public:
    Output(ushort relayNum, NCD2Relay* ncd2RelayPtr);
    ushort relayNum;
    OutputStatus outputStatus;
    int setOutputOn(void);
    int setOutputOff(void);
    void process(void);
    NCD2Relay* ncd2RelayPtr;
};
class HighWaterDetector {
public:
    HighWaterDetector(Device* device);
    NCD2Relay ncd2Relay;
    // Output output1;
    Output outputs[2];
    CloudMsgParser cloudMsgParser;
    Device * devicePtr;
};

Output::Output(ushort relayNum, NCD2Relay* ncd2RelayPtr2)
    : relayNum(relayNum), ncd2RelayPtr(ncd2RelayPtr2)
{
}
HighWaterDetector::HighWaterDetector(Device* device)
    : ncd2Relay(),
      outputs{Output(1, &ncd2Relay), Output(2, &ncd2Relay)},
      cloudMsgParser(),
      devicePtr(device)
{
}

Live Demo:

Without C++11, you need to create a default constructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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