缺少类模板的参数列表
Argument list for class template is missing
我有一个奇怪的问题,我不太确定是什么问题。我正在创建一个名为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,它也可以正常工作。
如果您能对问题的根源提供任何帮助,我将不胜感激。
谢谢。
首先,您应该如何为类模板的成员函数提供定义:
#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上看到这个Q&A。
在提供定义的同时,如果您正在使用模板,也要在您的类
中提到它们#include "LinkedArrayList.h"
template<typename ItemType>
void LinkedArrayList<ItemType>::insert (int index, const ItemType& item)
{}
相关文章:
- 概念中的cv限定符需要表达式参数列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题
- 错误 没有与参数列表匹配的重载函数"getline"实例
- std::vector 没有重载函数的实例与参数列表匹配
- 模板参数列表中的 false 在模板初始化期间计算为什么?
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 构造函数/函数声明参数列表中的统一初始化
- 模板化检查是否存在带有参数列表的类成员函数?
- 如何将类成员方法的参数列表自动填充写入可变参数?
- 带有整数的变量参数列表
- 转发变量参数列表以模拟 std::thread
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- C++-将具有引用的长参数列表重构为结构
- 缺少别名模板C++参数列表
- C++如果两个模板函数都与参数列表匹配,将调用哪个模板
- "extern"声明以及带有和不带参数列表的类模板实例的后续定义
- 使用显式模板参数列表和 [temp.arg.explicit]/3 的函数调用的演绎失败
- 没有函数模板的实例与我不知道为什么的参数列表匹配
- 我可以使用宏自动构建参数列表吗?
- 类成员函数参数列表是否可以依赖于模板参数?