Undefined Reference

I a trying to create a set of wrapper libraries around existing libraries with some common code that I use across projects. The tried to build a library on the OneWire library to see if the idea is feasible. The first problem with OneWire is it didn’t have a zero argument constructor, so I built one of those and a begin method to do the work that was done in the constructor.

Once I got it allmost compiled, I guess the last step of the compiler gives me the following errors.

Thermo\Thermo.cpp.o: In function Thermo::display()': C:\Arduino\arduino-1.0\libraries\Thermo/Thermo.cpp:38: undefined reference to OneWire::crc8(unsigned char*, unsigned char)’
Thermo\Thermo.cpp.o: In function Thermo::begin()': C:\Arduino\arduino-1.0\libraries\Thermo/Thermo.cpp:19: undefined reference to OneWire::begin(unsigned char)’
Thermo\Thermo.cpp.o: In function Thermo': C:\Arduino\arduino-1.0\libraries\Thermo/Thermo.cpp:11: undefined reference to OneWire::OneWire()’

Where these are the only references to the OneWire Instance variable

My Header looks like

#ifndef Thermo_h
#define Thermo_h

#include <Arduino.h>
#include "../OneWire/OneWire.h"

class Thermo
{
  public:
    Thermo(uint8_t pin, int count);
    void begin();
    void addThermo(byte *thermo);
    void display();

  private:
    uint8_t _pin;
    int _count; // the number allocated
    int _index; // the actual number added
    byte **_thermo;
    OneWire _ow;
};
#endif

and the implementation is

#include "Thermo.h"
#include "../OneWire/OneWire.h"

#define thermoSize 8

Thermo::Thermo(uint8_t pin, int count) {
    pinMode(pin, OUTPUT);
    _pin = pin;
    _count = count;
    _index = 0;
}

void Thermo::begin(){
  _ow.begin((uint8_t)_pin);
  //_ow =(OneWire)new OneWire((uint8_t)_pin);
  _thermo = (byte**) malloc(_count * sizeof(byte*));
  for(int row = 0; row < _count; row++){
    _thermo[row] = (byte *)malloc(thermoSize * sizeof(byte));
  }
}

void Thermo::addThermo(byte *thermo){
  if(_index > _count){
    return;
  }
  for(int i= 0; i < thermoSize; i++){
    ((byte*)_thermo[_index])[i] = thermo[i];
  }
  display();
}

void Thermo::display(){
  if (_index < _count && 
  OneWire::crc8((byte*)_thermo[_index], 7) != ((byte*)_thermo[_index])[7]){
    Serial.println("but it doesn't have a valid CRC!");
  } 
  else {
    for (int i = 0; i < thermoSize; i++){
      if (((byte*)_thermo[_index])[i] < 9){
        Serial.print("0");
      }
      Serial.print(((byte*)_thermo[_index])[i],HEX);
      if (i<7){
        Serial.print("-");
      }
    }
    Serial.println("");
  }
}

I have tried to find somewhere where it explains what is going on and couldn’t find anything useful.
If anyone can give e a pointer, either in what to change or where to look, I would appreciate it

Thanks

You have to include OneWire.h in your main sketch, or the arduino IDE won’t link it in.

Also, it’s not a great idea to #include “…/OneWire/OneWire.h” because there are two places people can put libraries. Just say #include <OneWire.h> and the IDE will tell the preprocessor where to find it.

Thank you.

I put the relative path to the OneWire library in because I couldn’t figure out how to make it work.

I appreciate your help.

I have a very basic question. In order to make this work in general, does the library I want to wrap has to provide a constructor with no arguments? For example SoftwareSerial does not provide such a constructor.

Is this true?

In order to make this work in general, does the library I want to wrap has to provide a constructor with no arguments?

No, but the wrapper class can't have a no argument constructor, either, then, unless it is to make assumptions about what to pass to the wrapped class's constructor.

If you have a class, Wrapper, you can have a constructor, Wrapper(int rxPin, int txPin), and have that constructor invoke the wrapped class's constructor at the same time.

Wrapper::Wrapper(int rxPin, int txPin) : _serial(SoftwareSerial(rxPin, txPin))
{
  // Do whatever else the constructor needs to do
}

where _serial is an member of the Wrapper class, of type SoftwareSerial.

Now I can do almost all object oriented code for y arduino projects.

Thanks for your help.

I guess I need to learn c++ better.