Saving pointer to class instance in another class?

OK, I’m stumped… I’ve got a sketch that uses OneWire devices. I’m making a library that will support a real-time clock chip (DS2417) on the OneWire net. So the library class needs to know and remember the user’s OneWire instance so it can talk to the clock chip. I’ve stripped the code to its essentials below:

// DS2417RTC.h - library for DS2417 RTC  -------------------------
#include <Time.h>
#include <OneWire.h>

class DS2417RTC {
  static OneWire* dsx;         // Private variable
  
  public:
    DS2417RTC(OneWire*);       // Constructor
    static time_t get();       // Member function
};


// Here's the DS2417RTC.ccp file-----------------------------------
#include <Time.h>
#include <OneWire.h>
#include "DS2417RTC.h"

DS2417RTC::DS2417RTC(OneWire* dsp) {
  dsx = dsp;          // Save pointer to main's OneWire instance
}

time_t DS2417RTC::get() {
  uint8_t rom[8] = {0x27, 0xAB, 0x5B, 0x22, 0, 0, 0, 0x6A}; // Clock chip addr
  
  dsx->select(rom);  // Represents several OneWire function calls
  return 123;
}

// Here's the Arduino sketch file (xxx.pde)------------------------
#include <Time.h>
#include "DS2417RTC.h"
#include <OneWire.h>

OneWire ds(10);       // One-Wire net on pin 10

DS2417RTC RTC(&ds);   // Use DS2417 Real-Time Clock 

void setup() {
}

void loop() {
  time_t t = RTC.get();
}


// Here's the error output:
//
DS2417RTC.cpp.o: In function `DS2417RTC':
(file path removed) /DS2417RTC.cpp:7: undefined reference to `DS2417RTC::dsx'
(file path removed) /DS2417RTC.cpp:7: undefined reference to `DS2417RTC::dsx'

DS2417RTC.cpp.o: In function `DS2417RTC::get()':
(file path removed) /DS2417RTC.cpp:13: undefined reference to `DS2417RTC::dsx'
(file path removed) /DS2417RTC.cpp:13: undefined reference to `DS2417RTC::dsx'

What’s he complaining about?

Thanks in advance for your attention!

73,
Gary

Oh, and the "get()" function can't have any parameters because it needs to be given to the Time routines so Time can get sync occasionally and Time can't supply parameters.

73, Gary

Static members of a class can not access member variables of instances of that class. For which instance of the class should it use the dsx pointer?

Add:

OneWire * DS2417RTC::dsx = 0;

In the cpp after the #includes

That did it, AlphaBeta! Errors are gone… now to make it work!

You guys are GOOD, and FAST too! 15 minutes, question to answer!

73,
Gary

One caveat to AlphaBeta's suggestion. You should only ever have one instance of the DS2417RTC class at a time. Otherwise the same dsx pointer will be used by all instances, and that is NOT what you want.

To my defense, it was not a suggestion but a fix ;)

If you need to address multiple OneWire objects (can you have more than one anyway? Maybe the Mega?) you cant use static.

To my defense, it was not a suggestion but a fix

I wasn't arguing that it wasn't a fix, or that it wouldn't work. I just pointed out that all instances of the class then access the same dsx pointer, too.

If, by its nature, the class only makes sense being instanced once, then this is not a problem.

But, if multiple instances of a class are possible, as in LED and motor classes, then, it could be a problem, and the use of static methods in the class needs to be avoided.