Having a lot of trouble creating a library

Hey everyone - for a project I’m working on I need to have several small FIFO buffers and I thought I’d try to implement it as a C++ class to get some experience with that aspect of C++. Unfortunately I’m having a terrible time trying to get the library to work in a sketch. Here is the code in question, first the header:

#include "WProgram.h"

#ifndef Buffer_h
#define Buffer_h

struct BufferStructure;
typedef struct BufferStructure *Buffer;

class FIFOBufferClass {

   byte CheckEmpty(Buffer B);
   byte CheckFull(Buffer B);
   Buffer CreateBuffer(byte Size);
   void DisposeBuffer(Buffer B);
   void EmptyBuffer(Buffer B);
   void LoadBuffer(byte data, Buffer B);  
   byte ReadBuffer(Buffer B);
    byte Succ(byte Value, Buffer B);



And the .cpp file:

#include <stdlib.h>
#include "WProgram.h" 
#include "Buffer.h"

#define MIN_SIZE 5

struct BufferStructure
  byte Capacity;
  byte Front;
  byte Rear;
  byte Size;
  byte *Array;

byte FIFOBufferClass::CheckEmpty(Buffer B)
  return B->Size == 0; 

byte FIFOBufferClass::CheckFull(Buffer B)
  if (B->Size == B->Capacity)
  return 1;

Buffer FIFOBufferClass::CreateBuffer(byte Size)
  Buffer B;
  if (Size < MIN_SIZE)
      Serial.print("Buffer Size Too Small!");  

  B = (Buffer)malloc(sizeof(struct BufferStructure));
  if (B == NULL)
      Serial.print("Out of Memory!");
  B->Array = (byte*)malloc(sizeof(byte)*Size);
  if (B->Array == NULL)
      Serial.print("Out of Memory!");
  B->Capacity = Size;
  return B;
void FIFOBufferClass::DisposeBuffer(Buffer B)
  if (B != NULL)


void FIFOBufferClass::EmptyBuffer(Buffer B)
   B->Size = 0;
   B->Front = 1;
   B->Rear = 0; 

void FIFOBufferClass::LoadBuffer(byte data, Buffer B)
      Serial.print("Buffer is full!");
       B->Rear = Succ(B->Rear, B); 
       B->Array[B->Rear] = data;  

byte FIFOBufferClass::ReadBuffer(Buffer B)
    byte data; 

       Serial.print("Buffer is empty!"); 
        data = B->Array[B->Front];
        return data;
 byte FIFOBufferClass::Succ(byte Value, Buffer B)
    if (++Value == B->Capacity)
        Value = 0;
    return Value;   

The problem is when I include this library in a sketch, create an instance of the class, and then try to call any member function of the class I always get the following kind of error: “undefined reference to 'FIFOBufferClass::CreateBuffer(unsigned char)”, no matter what function I try to call. Can anyone see where I’ve gone wrong? :’(

Edit: Here is the main sketch:

#include <Buffer.h>

void setup()                    
  FIFOBufferClass TestClass;
  Buffer TestBuffer;
  TestBuffer = TestClass.CreateBuffer(10);

void loop()                     

I managed to get it to work after digging around the forums a bit - I tried deleting the object file in the library directory, then restarting the Arduino environment. No more errors using the functions except for some warnings on compile which I'm going to have to correct in the main code.


Would you mind posting the final (and working) code for your FIFO buffer as well, I'm really interested in the functionality and not even a real newbie in C.

Thanks, Dani

Not to be too picky, but this is not a very good class. It's a collection of functions pretending to be a class.

If it were a class, it would have a constructor and a destructor. None of the methods shown would need to have a Buffer passed to them.

You don't show how you use this class. Do you create an instance of the class? If you create one like this:

FIFOBufferClass myBuffer;

You should be able to determine if myBuffer has anything in it:

byte empty = myBuffer.CheckEmpty();

Why do CheckEmpty and CheckFull return byte values, instead of boolean? The buffer is either empty or not, or full or not. As it is, I need to look at the source code to see that I'm not going to get back a percentage full.

One last nitpick. FIFOBufferClass is redundant as the name of a class. FIFOBuffer would be a better name. By definition, it's a class. Only Microsoft programmers need to be reminded of that fact every time they use a class.

Thanks for your considerations! Do you recommend any already contributed, well designed fifobuffer class for the arduino? Dani