Cryptic Templated Class Error

Hi all, I searched around the forums, but didn’t find anything similar… I’m getting a weird, cryptic error on my class I wrote. I’m trying to write an asynchronous buffered morse code output. So it takes in some strings, pushes them into the outputQ, then as necessary it takes the first character off outputQ, translates it to Morse code, then pushes it to the morseQ. Every few ticks, it takes the first item (HIGH or LOW for whichever the output pin should be) off morseQ, and outputs it. But the problem I am having is getting my queues to initialize. Any help is greatly appreciated. I am probably missing something really basic!

The error I get says:

instantiated from here
  In constructor 'queue<T>::queue(int) [with T = char]':
instantiated from here In constructor 'queue<T>::queue(int) [with T = unsigned char]':

The class file is “queue.h”:

#ifndef queue_h
#define queue_h

#include "WProgram.h"

template <typename T>
class queue {
  T*  storage;
  int maxSize;
  int frontPosition;
  int currentSize;

  public:
  queue(int max_size) {
    maxSize = max_size;
    storage = malloc(maxSize * sizeof(T));
    //memset(storage, 0, maxSize * sizeof(T));
  }

  ~queue() {
    memset(storage, 0, maxSize * sizeof(T));
    free(storage);
  }

  T front() {
    return storage[frontPosition];
  }

  bool push(T x) {
    if (!full()) {
      storage[(frontPosition+currentSize) % maxSize] = x; // insert the data at the tail
      currentSize++;
      return true;
    } else {
      return false;
    }
  }

  bool pop() {
    if (!empty()) {
      storage[frontPosition] = 0; // clear the front
      frontPosition = (frontPosition + 1) % maxSize; // advance the queue pointer
      currentSize--;
      return true;
    } else {
      return false;
    }
  }

  bool empty() {
    return (currentSize <= 0);
  }

  bool full() {
    return (currentSize >= maxSize);
  }
};

#endif

and the section that gets highlighted when trying to compile is below:

queue<char> outputQ(256); // This is the line that it highlights for the error
queue<byte> morseQ(24);

Thanks so much for any help anyone can offer!

Right after posting this, I found where the problem is. When I comment out the 'malloc' line in the class constructor, it compiles fine.

The offending section:

class queue {
  T*  storage;
  int maxSize;
  int frontPosition;
  int currentSize;

  public:
  queue(int max_size) {
    maxSize = max_size;
    storage = malloc(maxSize * sizeof(T)); // This line breaks things!
    //memset(storage, 0, maxSize * sizeof(T));
  }

So, I am not quite sure how to allocate my 'storage' variable for the queue.

any advice, please?

Thanks!

What about having the size as part of the template?

template <typename T, int size>
class queue {
  T  storage[size];
};

D'oh! That works a treat! Can't believe I didn't think of that!

Thanks again!