缺少类模板的参数列表 [英] Argument list for class template is missing

查看:7278
本文介绍了缺少类模板的参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个好奇的问题,我不太确定问题是什么。我创建一个叫LinkedArrayList的类,使用typename模板,如下面的代码所示:

I'm having a curious issue, and I'm not quite sure what the issue is. I'm creating a class called LinkedArrayList that uses a typename template, as shown in the code below:

#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中:

Now, this doesn't give any errors or problems. However, creating the functions in the .cpp file gives me the error "Argument list for class template 'LinkedArrayList' is missing." It also says that ItemType is undefined. Here is the code, very simple, in the .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改为ints,它不会给出任何错误。另外,如果我只是在.h中的所有代码,而不是一个单独的.cpp,它的工作也很好。

It looks like it has something to do with the template, because if I comment it out and change the ItemTypes in the functions to ints, it doesn't give any errors. Also, if I just do all the code in the .h instead of having a separate .cpp, it works just fine as well.

任何帮助的源

感谢。

推荐答案

所有,这是你应该如何为类模板的成员函数提供一个定义:

First of all, this is how you should provide a definition for member functions of a class template:

#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;}

其次,这些定义不能放在 .cpp 文件中,因为编译器不能从调用点隐式实例化它们。例如,请参阅 此问答在StackOverflow上

Secondly, those definitions cannot be put in a .cpp file, because the compiler won't be able to instantiated them implicitly from their point of invocation. See, for instance, this Q&A on StackOverflow.

这篇关于缺少类模板的参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆