C++ 将泛型迭代器传递给函数cpp
尝试使用模板发送迭代器函数,该函数可以获取任何迭代器(来自数组、队列等)。[在示例中,我发送向量] 错误:第15行未编译:C++ 将泛型迭代器传递给函数cpp,c++,C++,尝试使用模板发送迭代器函数,该函数可以获取任何迭代器(来自数组、队列等)。[在示例中,我发送向量] 错误:第15行未编译: ExampleVector <int> vec(values.begin(), values.end())") ExampleVector向量(values.begin(),values.end())) 模板 类ExampleVector//新类 { 模板//泛型迭代器 ExampleVector(InputIterator&first,Input
ExampleVector <int> vec(values.begin(), values.end())")
ExampleVector向量(values.begin(),values.end()))
模板
类ExampleVector//新类
{
模板//泛型迭代器
ExampleVector(InputIterator&first,InputIterator&last)//构造函数(不执行任何操作)
{
}
};
int main()
{
/*创建值*/
std::向量val{4,8,12};
/*创建vec*/
示例向量向量(val.begin(),val.end());
}
修复一个又一个编译时错误后,我得到以下代码:
#include <iostream>
#include <vector>
template <typename ExampleVectorType>
class ExampleVector //new class
{
public:
template <class InputIterator> // generic iterator
ExampleVector (const InputIterator& first, const InputIterator& last) // constructor (do nothing)
{
}
};
int main()
{
/* Create the values */
std::vector<int> val{4, 8, 12};
/* Create the vec */
ExampleVector <int> vec(val.begin(), val.end());
}
#包括
#包括
模板
类ExampleVector//新类
{
公众:
模板//泛型迭代器
ExampleVector(const-InputIterator&first,const-InputIterator&last)//构造函数(不执行任何操作)
{
}
};
int main()
{
/*创建值*/
std::向量val{4,8,12};
/*创建vec*/
示例向量向量(val.begin(),val.end());
}
错误和解决方案如下:
值
==>val
public:
const
lvalue引用虽然问题似乎相当基本,但最初发布的代码中变量的名称有误,这意味着即使更正了变量名称,显示的代码仍然不是您试图编译的真实代码,问题可能完全是其他问题。这就是stackoverflow.com告诉您显示一个,ins的原因代替假代码。@mch谢谢,已编辑。键入错误。
#include <iostream>
#include <vector>
template <typename ExampleVectorType>
class ExampleVector //new class
{
public:
template <class InputIterator> // generic iterator
ExampleVector (const InputIterator& first, const InputIterator& last) // constructor (do nothing)
{
}
};
int main()
{
/* Create the values */
std::vector<int> val{4, 8, 12};
/* Create the vec */
ExampleVector <int> vec(val.begin(), val.end());
}