Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • I'm a little confused. If output has a default constructor, how is it possible you can do "outputs[0] = Output(1, &ncd2Relay);" in your first solution? Commented Aug 11, 2016 at 4:31
  • The outputs[] array elements first get default constructed before the HighWaterDetector constructor is entered. That is what the compiler error is complaining about - a missing Output default constructor. So add one. Then, once the constructor is entered, new Output objects are constructed using your 2-input constructor, and then are copy-assigned into the array. The compiler will have generated a default copy constructor and copy assignment operator for you. Or you can define your own if needed (see Rule of Three). Commented Aug 11, 2016 at 4:34
  • Interesting... so Output can have two constructors, a default and another? How does it know to use the default first? Commented Aug 11, 2016 at 4:38
  • @Felix: a class can have as many constructors as you want, as long as they take different inputs. A default constructor has special meaning, as it is called automatically when an instance of the class is created without any input values. When you declare an array like Output output[2];, its elements still have to be constructed like any other variable. Since the array is a member of HighWaterDetector, you have to provide the array element constructor inputs in the HighWaterDetector constructor's initialization list, otherwise they get default constructed instead. Commented Aug 11, 2016 at 4:47
  • @Felix: the compiler automatically generates a default constructor for you - unless - you define a custom constructor instead, which you did. Commented Aug 11, 2016 at 4:49