编译错误-如何在c++模板函数中传递普通参数和模板参数
compiler errors - How to pass normal param as well as template param in a template function in C++?
在命名空间myNamespace中有一个模板函数(如下所示):
template <typename setX>
void getRandomItems(NaturalNumber size, setX &random, setX &items)
{
assert(size <= items.size());
//set of randomly selected indices for items
set<NaturalNumber> index;
NaturalNumber r, i;
while(index.size() < size)
{
r = unifRand(0,items.size()-1);
index.insert(r);
}
typename setX::iterator it, sit = items.begin();
for(i = 0, it = index.begin(); it != index.end(); it ++)
{
//find the r-th elt in index
r = *it;
for(; i < r; i ++)
sit++;
random.insert(*sit);
}
}
然而,每当我调用这个函数,我得到这些错误:
<>之前generic.h:在函数' void myNamespace::getRandomItems(NaturalNumber, setX&, setX&) [with setX = std::set我已经尝试了所有的组合,但没有运气,请帮助我!!
setX
不是NaturalNumber
的集合,所以当你说it = index.begin()
时迭代器是不兼容的。你可以让it
成为set<NaturalNumber>
的迭代器,我不太明白你在这里真正想做什么。
我还注意到,在你的内部循环中,你没有做任何检查,以确保sit
不会跑掉它的集合的末尾。
您正在尝试分配不兼容的迭代器。
也许你的意思是
set<NaturalNumber>::iterator it;
typename setX::iterator sit = items.begin();
不是
typename setX::iterator it, sit = items.begin();
相关文章:
- C++声明模板参数阴影模板参数错误
- 为什么 CRTP 模板C++给出无效参数错误?
- (C/C++)fscanf_s从txt文件以字符形式读取数组时缺少整数参数错误
- 参数错误可能与类型不匹配有关?
- printf 和 strftime 的参数错误无效
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 如何修复"没有重载函数需要 2 个参数"错误C++
- 术语不计算为函数采用 1 个参数错误?
- 从带有 getline() 的文件读入一行上有多个信息得到无效参数错误
- 为什么我不能像使用 std::string::size_type 那样使用 QList::size_type?(模板参数错误)
- cudaFreeHost() 无效参数错误
- 精神语法不会编译:函数模板参数错误?
- C 功能具有参考参数错误的迭代器错误.寻求解释
- 使用getDefaultCommConfig使用无效的参数错误
- C++ 可变参数模板和模板模板参数:错误:模板参数列表中参数 1 处的类型/值不匹配
- 使用SWIG生成的Python库时,向量分配器参数错误
- 错误C2664:无法转换参数错误
- C 作为参数错误的功能
- 命令行参数错误
- 增强Python参数错误