Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 缺少类模板的参数列表_C++_Templates_Typename - Fatal编程技术网

C++ 缺少类模板的参数列表

C++ 缺少类模板的参数列表,c++,templates,typename,C++,Templates,Typename,我有一个奇怪的问题,我不太确定问题是什么。我正在创建一个名为LinkedAryList的类,该类使用typename模板,如下代码所示: #pragma once template <typename ItemType> class LinkedArrayList { private: class Node { ItemType* items; Node* next; Node* prev; int capacity; int siz

我有一个奇怪的问题,我不太确定问题是什么。我正在创建一个名为LinkedAryList的类,该类使用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);
};
它看起来与模板有关,因为如果我注释掉它并将函数中的ItemTypes更改为int,它不会给出任何错误。另外,如果我只执行.h中的所有代码而不是单独的.cpp,那么它也可以正常工作

如果您能提供有关问题根源的任何帮助,我们将不胜感激


谢谢。

首先,以下是如何为类模板的成员函数提供定义:

#include "LinkedArrayList.h"

template<typename ItemType>
void LinkedArrayList<ItemType>::insert (int index, const ItemType& item)
{}

template<typename ItemType>
ItemType LinkedArrayList<ItemType>::remove (int index)
{return ItemType();}

template<typename ItemType>
int LinkedArrayList<ItemType>::find (const ItemType& item)
{return -1;}
#包括“LinkedArrayList.h”
模板
void linkedarylist::insert(int索引、常量ItemType和item)
{}
模板
ItemType LinkedArray列表::删除(int索引)
{return ItemType();}
模板
int linkedarylist::find(const ItemType&item)
{return-1;}
其次,这些定义不能放在
.cpp
文件中,因为编译器无法从调用点隐式地实例化它们。例如,请参见

#include "LinkedArrayList.h"

template<typename ItemType>
void LinkedArrayList<ItemType>::insert (int index, const ItemType& item)
{}

template<typename ItemType>
ItemType LinkedArrayList<ItemType>::remove (int index)
{return ItemType();}

template<typename ItemType>
int LinkedArrayList<ItemType>::find (const ItemType& item)
{return -1;}