使用类模板需要模板参数列表
Use of class template requires template argument list?
我有三个错误:
1( 'KVList::add' : 无法将函数定义与现有声明匹配
2( 'KVList::replace' :无法将函数定义与现有声明匹配
3( "KVList":使用类模板需要模板参数列表
这是我的代码
KVList.h
template <class K, class V, int N>
class KVList
{
K k[N];
V v[N];
size_t count;
public:
KVList();
size_t size() const;
const K& key(int i) const;
const V& value(int i) const;
KVList& add(const K&, const V&);
int find(const K&k) const;
KVList& replace(int i, const K& kk, const V& vv);
};
KVList.cpp
#include "KVList.h"
template <class K, class V, int N> KVList<K, V, N>::KVList() : count(0)
{
}
template <class K, class V, int N> size_t KVList<K, V, N>::size() const
{
return count;
}
template <class K, class V, int N> const K& KVList<K, V, N>::key(int i) const
{
return k[i];
}
template <class K, class V, int N> const V& KVList<K, V, N>::value(int i) const
{
return v[i];
}
template <class K, class V, int N> KVList& KVList<K, V, N>::add(const K&, const V&)
{
if(count<N)
{
k[count] = kk;
v[count] = vv;
count++;
}
return *this;
}
template <class K, class V, int N> int KVList<K, V, N>::find(const K&k) const
{
for(int ret = 0; ret < count; ret++)
{
if(kk == k[ret])
{
return ret;
}
}
return 0;
}
template <class K, class V, int N> KVList& KVList<K, V, N>::replace(int i, const K& kk, const V& vv)
{
if (i < count)
{
k[i] = kk, v[i] = vv;
}
return *this;
}
请帮忙(因为我不知道如何解决这三个错误,因为我尝试了一切(!
您需要将KVList<K, V, N>&
编写为返回类型。在此上下文中,不能省略模板参数。所以你必须写:
template <class K, class V, int N>
KVList<K, V, N>& KVList<K, V, N>::add(const K&, const V&)
等等。但是,如果使用尾随返回类型,则可以省略模板参数。
template <class K, class V, int N>
auto KVList<K, V, N>::add(const K&, const V&) -> KVList&
返回模板对象引用需要参数:
template <class K, class V, int N> KVList<K, V, N>& KVList<K, V, N>::add(const K&, const V&) {
^^^^^^^^^^^^^^^^
...
}
相关文章:
- 概念中的cv限定符需要表达式参数列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题
- 错误 没有与参数列表匹配的重载函数"getline"实例
- std::vector 没有重载函数的实例与参数列表匹配
- 模板参数列表中的 false 在模板初始化期间计算为什么?
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 构造函数/函数声明参数列表中的统一初始化
- 模板化检查是否存在带有参数列表的类成员函数?
- 如何将类成员方法的参数列表自动填充写入可变参数?
- 带有整数的变量参数列表
- 转发变量参数列表以模拟 std::thread
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- C++-将具有引用的长参数列表重构为结构
- 缺少别名模板C++参数列表
- C++如果两个模板函数都与参数列表匹配,将调用哪个模板
- "extern"声明以及带有和不带参数列表的类模板实例的后续定义
- 使用显式模板参数列表和 [temp.arg.explicit]/3 的函数调用的演绎失败
- 没有函数模板的实例与我不知道为什么的参数列表匹配
- 我可以使用宏自动构建参数列表吗?
- 类成员函数参数列表是否可以依赖于模板参数?