我遇到了一个奇怪的问题,但是我不太确定问题出在哪里。我正在创建一个名为LinkedArrayList的类,它使用了typename模板,如下面的代码所示:
#pragma once
template <typename ItemType>
class LinkedArrayList
{
private:
class Node {
ItemType* items;
Node* next;
Node* prev;
int capacity;
int size;
};
Node* head;
Node* tail;
int size;
public:
void insert (int index, const ItemType& item);
ItemType remove (int index);
int find (const ItemType& item);
};
现在,这段代码没有任何错误或问题。但是,在.cpp文件中创建函数会给我带来错误:“缺少类模板LinkedArrayList的参数列表。”它也说ItemType未定义。以下是非常简单的.cpp代码:
#include "LinkedArrayList.h"
void LinkedArrayList::insert (int index, const ItemType& item)
{}
ItemType LinkedArrayList::remove (int index)
{return ItemType();}
int find (const ItemType& item)
{return -1;}
看起来这与模板有关,因为如果我将其注释并将函数中的ItemTypes更改为int,则不会出现任何错误。此外,如果我将所有代码放在.h文件中而不是使用单独的.cpp文件,它也可以正常工作。
非常感谢您提供有关问题来源的任何帮助。
谢谢。