C++: objects as private variables of other objects

The following code is giving me an error that I don't understand. Can't I make an object a private variable of another object without using inheritance? Is it just because there is a non-trivial constructor? (I'm pretty sure I've seen objects with String member variables, haven't I?)

#include <Adafruit_NeoPixel.h>

class singleNeo {
  private:
    Adafruit_NeoPixel pixs(1, NEOPIN, NEO_GRB + NEO_KHZ800);
    uint_fast8_t pin;
  public:
    singleNeo(uint_fast8_t pinNo): pin(pinNo) {}
    void RGB(uint_fast8_t redv, uint_fast8_t greenv, uint_fast8_t bluev) {
      pixs->setPixelColor(0, pixs->Color(redv, greenv, bluev));
      pixs->show();
    }
};

void setup() {}
void loop() {}
"D:\\arduino-1.8.13\\portable\\packages\\adafruit\\tools\\arm-none-eabi-gcc\\9-2019q4/bin/arm-none-eabi-g++" -mcpu=cortex-m0plus -mthumb -c -g -Os -Wall -Wextra -Werror=return-type -Wno-expansion-to-defined -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD "-D__SKETCH_NAME__=\"\"\"singleneo_simple.ino\"\"\"" -DF_CPU=48000000L -DARDUINO=10813 -DARDUINO_QTPY_M0 -DARDUINO_ARCH_SAMD -DARDUINO_SAMD_ADAFRUIT -D__SAMD21E18A__ -DCRYSTALLESS -DADAFRUIT_QTPY_M0 -DARM_MATH_CM0PLUS -DUSB_VID=0x239A -DUSB_PID=0x80CB -DUSBCON -DUSB_CONFIG_POWER=100 "-DUSB_MANUFACTURER=\"Adafruit\"" "-DUSB_PRODUCT=\"QT Py M0\"" "-ID:\\arduino-1.8.13\\portable\\packages\\adafruit\\hardware\\samd\\1.7.13/libraries/Adafruit_TinyUSB_Arduino/src/arduino" -Os -D__SAMD21E18A__ -DCRYSTALLESS -DADAFRUIT_QTPY_M0 -DARM_MATH_CM0PLUS -DUSB_VID=0x239A -DUSB_PID=0x80CB -DUSBCON -DUSB_CONFIG_POWER=100 "-DUSB_MANUFACTURER=\"Adafruit\"" "-DUSB_PRODUCT=\"QT Py M0\"" "-ID:\\arduino-1.8.13\\portable\\packages\\adafruit\\hardware\\samd\\1.7.13/libraries/Adafruit_TinyUSB_Arduino/src/arduino" "-ID:\\arduino-1.8.13\\portable\\packages\\adafruit\\tools\\CMSIS\\5.4.0/CMSIS/Core/Include/" "-ID:\\arduino-1.8.13\\portable\\packages\\adafruit\\tools\\CMSIS\\5.4.0/CMSIS/DSP/Include/" "-ID:\\arduino-1.8.13\\portable\\packages\\adafruit\\tools\\CMSIS-Atmel\\1.2.2/CMSIS/Device/ATMEL/" "-ID:\\arduino-1.8.13\\portable\\packages\\adafruit\\hardware\\samd\\1.7.13\\cores\\arduino" "-ID:\\arduino-1.8.13\\portable\\packages\\adafruit\\hardware\\samd\\1.7.13\\variants\\qtpy_m0" "-ID:\\arduino-1.8.13\\portable\\sketchbook\\libraries\\Adafruit_NeoPixel" "d:\\tmp\\Arduino1.8.13Build\\sketch\\singleneo_simple.ino.cpp" -o "d:\\tmp\\Arduino1.8.13Build\\sketch\\singleneo_simple.ino.cpp.o"
singleneo_simple:5:28: error: expected identifier before numeric constant
    5 |     Adafruit_NeoPixel pixs(1, NEOPIN, NEO_GRB + NEO_KHZ800);
      |                            ^
singleneo_simple:5:28: error: expected ',' or '...' before numeric constant
(and a bunch more errors since pixs didn't get defined)

Perhaps, try putting its initialization in the initializer list of your class's constructor.

Also, where is 'NEOPIN' defined?

Also:

'pixs' is not a pointer. Use pixs.show(), etc.

ditto..

#include <Adafruit_NeoPixel.h>

class singleNeo {
  private:
    Adafruit_NeoPixel pixs;
    uint_fast8_t pin;
  public:
    singleNeo(uint_fast8_t pinNo): pin(pinNo) {
      pixs.setPin(pin);
      pixs.updateType(NEO_GRB + NEO_KHZ800);
      pixs.updateLength(1);
      pixs.begin();
    }
    void RGB(uint_fast8_t redv, uint_fast8_t greenv, uint_fast8_t bluev) {
      pixs.setPixelColor(0, pixs.Color(redv, greenv, bluev));
      pixs.show();
    }
};

singleNeo neo(6);

void setup() {
}
void loop() {
  neo.RGB(0, 0, 0);
  delay(1000);
  neo.RGB(255, 0, 0);
  delay(1000);
  neo.RGB(0, 255, 0);
  delay(1000);
  neo.RGB(0, 0, 255);
  delay(1000);
}

compiles..
simmed here..

have fun.. ~q

This is invalid syntax: default member initializers are specified using curly braces, not parentheses.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.