Use instance in constructor

Hi all,

I’m trying to use an instance as an argument to create a new instance (through a constructor), however I can’t get it to compile.

.ino file

#include <Adafruit_NeoPixel.h>

#include "LedBoard.h"

... defines ...

Adafruit_NeoPixel board1 = Adafruit_NeoPixel(TOTAL_LED_BOARD, PIN_LED_BOARD, NEO_GRB + NEO_KHZ800);

LedBoard ledBoard1 = LedBoard(board1);

LedBoard.h

#ifndef LedBoard_h
#define LedBoard_h

#include <Arduino.h>
#include <Adafruit_NeoPixel.h>

/**
 * Led Board class header for generating texts on a 32x8 WS2812b led board
 **/
class LedBoard {

	public:
		LedBoard(Adafruit_NeoPixel board);

};


#endif

LedBoard.ccp

#include "LedBoard.h"
#include <Arduino.h>


/**
 * Constructor
 **/
void LedBoard::LedBoard(Adafruit_NeoPixel board) {

	_board = board;

}

This is the output error:

/var/folders/8p/b_5tx19j1bzbfhwt_f6yyq5r0000gn/T//cckHuour.ltrans0.ltrans.o: In function `global constructors keyed to 65535_0_led_board.ino.cpp.o.3163':
<artificial>:(.text.startup+0x2a4): undefined reference to `LedBoard::LedBoard(Adafruit_NeoPixel)'
/var/folders/8p/b_5tx19j1bzbfhwt_f6yyq5r0000gn/T//cckHuour.ltrans0.ltrans.o: In function `nextbyte20':
<artificial>:(.text.startup+0x8a2): undefined reference to `LedBoard::setF()'

I’ve tried including <Adafruit_NeoPixel.h> to the .ccp file or remove it from the .ino and/or header file, but doesn’t make any difference. Also saw some previews where they include a reference class Adafruit_NeoPixel on top, but doesn’t seems to be working either.

Can somebody give me an example how to do this in C++ (I’m more a PHP and Phyton person).

Thank you!

With kind regards,

Tim

Pass a pointer (or reference) to a Adafruit_NeoPixel object instead. Have a (private) member in your LedBoard class that’s a pointer (or reference) to a Adafruit_NeoPixel object.

Three problems that jumped out, there could be more:

  1. .ccp is not a valid extension for C++ files, the Arduino IDE will not compile it. You need .cpp.
  2. You have to include the class definition in your .cpp file (#include "LedBoard.h").
  3. The constructor in your .cpp file cannot have "void" in front of it.

gfvalvo also has a good point, keeping a copy to the NeoPixel object seems like an unnecessary waste of memory, and will probably produce unexpected results. (I'm not even sure if the Adafruit_NeoPixel class is copyable.)

Pieter

PieterP:
gfvalvo also has a good point, keeping a copy to the NeoPixel object seems like an unnecessary waste of memory, and will probably produce unexpected results. (I'm not even sure if the Adafruit_NeoPixel class is copyable.)

Pieter

Even if it compiles as copyable I wouldn't take a chance on having more than one copy. It can be very confusing even if the copy constructor is implemented properly.

PieterP:
Three problems that jumped out, there could be more:

  1. .ccp is not a valid extension for C++ files, the Arduino IDE will not compile it. You need .cpp.
  2. You have to include the class definition in your .cpp file (#include “LedBoard.h”).
  3. The constructor in your .cpp file cannot have “void” in front of it.

gfvalvo also has a good point, keeping a copy to the NeoPixel object seems like an unnecessary waste of memory, and will probably produce unexpected results. (I’m not even sure if the Adafruit_NeoPixel class is copyable.)

Pieter

Thanks Peter, that helped me a lot! Feel so stupid using .ccp instead of .cpp.

I think this is an assumption mistake of mine, as I’m trying to use the instance as a reference and not copy it. In PHP all objects/instances are copied as references, but as I can tell, that’s not the case in C++.

What’s the correct include syntax, should I use #include <Adafruit_NeoPixel.h> in both the .h and .cpp files?

I’ve used this for the constructor now, and it seems to compile fine, is this the correct syntax for using references?

/**
 * Constructor
 **/
LedBoard::LedBoard(Adafruit_NeoPixel& board) {

    _board = board;

}

Edit: seems the forum doesn’t like emoji’s very much

faim:
I've used this for the constructor now, and it seems to compile fine, is this the correct syntax for using references?

No, if you're going to pass a reference (instead of a pointer), then '_board' must be declared as a reference member of the LedBoard class. And, it must be bound to the parameter 'board' using an Initializer List.