Queueというクラス テンプレートを作成しましたが、タイプとして Worker という別のクラスへのポインタを使用してインスタンス化しようとすると問題が発生します。Queue<Worker*>
問題は、この具体的な Queue メソッドにあると思います。
// Add item to queue
template <typename Type>
bool Queue<Type>::enqueue(const Type& item)
{
if (isfull())
return false;
Node* add = new Node; // create node
// on failure, new throws std::bad_alloc exception
add->item = item; // set node pointers (shallow copying!!!!!)
add->next = nullptr;
items++;
if (front == nullptr) // if queue is empty,
front = add; // place item at front
else
rear->next = add; // else place at rear
rear = add; // have rear point to new node
return true;
}
型パラメーターがポインターの場合、プログラムのクラッシュを避けるために、アドレスではなく、ポイント先の値をコピーする必要があります (動的メモリ管理を使用しています)。
テンプレートでこれを解決する方法がわかりません。
何か助けはありますか?