Reference in a Class - to some other object

Hi,

I am trying to write my first Arduino library.
Everything was fine until I decided to try references.
In my class ‘MYCLASS’ I have a privat class variable _PCF where I want to keep a reference to some other class/object (PCF8574).

How I try to do it:

MYCLASS.h

#ifndef MYCLASS_h
#define MYCLASS_h

#include <Arduino.h>
#include <PCF8574.h>

class MYCLASS {
  public:
    MYCLASS();
    void begin(PCF8574& PCF);
    void test();   
    
  private:
    PCF8574 _PCF;
};
#endif

MYCLASS.cpp

#include "MYCLASS.h" 
#include <Arduino.h>
#include <PCF8574.h>

MYCLASS::MYCLASS();

void MYCLASS::begin(PCF8574& PCF) // referense using '&'
{
  _PCF = PCF;   
  _PCF.pinMode(0,OUTPUT);
  _PCF.pinMode(1,OUTPUT);   
} 

void MYCLASS::test()
{
  _PCF.digitalWrite(0,HIGH);
}

This is how I want to use it:

PCF8574 a;
MYCLASS mc;

void setup() {
    mc.begin(a);
    a.begin(b1000000);
    mc.test();
}

That does not work of course - I know… that would be to easy.
Could you please help me to solve this problem please!

Thanks!

What problem? I have no idea what your class PCF8574 does or in what way your proposed code fails.

This is how I want to use it

The _PCF variable is not a reference variable (there is no & in front of it).

You need to include header files for both classes in the sketch.

Like PaulS said you need to make the variable a reference. But you also need to initialise it, you must use the initialisation list:

void MYCLASS::begin(PCF8574& PCF)
{
_PCF = PCF;
_PCF.pinMode(0,OUTPUT);
_PCF.pinMode(1,OUTPUT);
}

void MYCLASS::begin(PCF8574& PCF) : _PCF( PCF )
{
_PCF.pinMode(0,OUTPUT);
_PCF.pinMode(1,OUTPUT);
}