I new to c++ and trying to understand exactly what is happening with my code.
These classes are both defined in their own header files. The code is below.
Queue:
template<class T> class Queue
{
public:
Queue(unsigned int size)
{
_buffer = new T[size]; //need to make sure size is a power of 2
_write = 0;
_read = 0;
_capacity = size;
}
/* other members ... */
private:
unsigned int _capacity;
unsigned int _read;
unsigned int _write;
T *_buffer;
};
Serial:
template<class T> class Queue;
template<class T> class Serial
{
public:
Serial(unsigned int buffer_size)
{
_queue = Queue<T>(buffer_size); //<---here is the problem
}
private:
Queue<T> _queue;
};
When I try to create an instance of Serial like this:
Serial<unsigned char> s = Serial<unsigned char>(123);
The compiler complains that there is no Queue constructor with zero arguments, at least that is what I think the errors means:
In instantiation of 'Serial<T>::Serial(unsigned int) [with T = unsigned char]': no matching function for call to 'Queue<unsigned char>::Queue()' ambiguous overload for 'operator=' in '((Serial<unsigned char>*)this)->Serial<unsigned char>::_queue = (operator new(16u), (((Queue<unsigned char>*)<anonymous>)->Queue<T>::Queue<unsigned char>(buffer_size), ((Queue<unsigned char>*)<anonymous>)))' (operand types are 'Queue<unsigned char>' and 'Queue<unsigned char>*') invalid user-defined conversion from 'Queue<unsigned char>*' to 'const Queue<unsigned char>&' [-fpermissive] invalid user-defined conversion from 'Queue<unsigned char>*' to 'Queue<unsigned char>&&' [-fpermissive] conversion to non-const reference type 'class Queue<unsigned char>&&' from rvalue of type 'Queue<unsigned char>' [-fpermissive]
When I add an empty constructor to Queue it compiles with no problems. When I step through with the debugger I see it is going into the constructor with parameters, not the empty one.
Why is this happening?