使用 for_each 时出错
我的代码中有以下 2 个函数:
bool num()
{
return 0;
}
void setDFS()
{
int i = 0;
project3::Graph<string, string> g1;
std::for_each(g1.Vertice1.begin(), g1.Vertice1.end(),num);
}
该函数的作用是对于向量 Vertice1 中的每个顶点,它现在必须将其编号设置为 0。一旦我开始图形遍历,稍后我会将 num 增加到遍历的计数。
编译时,我得到“ 错误 C2197:'bool (__cdecl *)(void)':调用参数太多”错误。
template <class VertexType, class EdgeType> class Vertex{
protected:
VertexType vertice;
EdgeType edge;
public:
};
std::vector<project3::Vertex<VertexType, EdgeType>*> Vertice1;
I have the following 2 functions in my code:
bool num()
{
return 0;
}
void setDFS()
{
int i = 0;
project3::Graph<string, string> g1;
std::for_each(g1.Vertice1.begin(), g1.Vertice1.end(),num);
}
What the function does is for each Vertice in vector Vertice1, it has to set its number to 0 for now. Once I start graph traversing, later on I would be incrementing the num to the traversed count.
While compiling, I am getting "
error C2197: 'bool (__cdecl *)(void)' : too many arguments for call" error.
template <class VertexType, class EdgeType> class Vertex{
protected:
VertexType vertice;
EdgeType edge;
public:
};
std::vector<project3::Vertex<VertexType, EdgeType>*> Vertice1;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
for_each 算法接收 一元函数 应具有以下签名:
其中 T 是 g1.Vertice1 向量的元素类型:
for_each algorithm receives a unary function which should have the following signature:
where T is the type of element of g1.Vertice1 vector:
摘自SGI 网站:
和
据我从文档中了解到, for_each 构造将迭代传递的集合并将当前元素传递给您的函数,然后您需要相应地更新传递的项目。
Taken from the SGI site:
And
As far as I understood from the documentation, the
for_each
construct will iterate over the passed collection and pass the current element to your function, in which you then need to update the passed item accordingly.根据我之前对同一程序/同一用户的经验,我会说 num 需要采用字符串参数,因为它需要进行操作(查看 std::for_each 的第三个参数的声明)标头中的 () 方法)。
另外,如果 num() 方法是对象的一部分,我认为它不会直接工作(因为 C++ 对成员函数的隐式“this”参数。)
一个可能的解决方案是围绕此函数的非成员包装器,像这样:
From a previous experience with what I think is the same program/same user, I would say that num needs to take a string argument, as it needs something to operate on (look at the declaration of the third argument to the std::for_each() method in your headers).
Also, if the num() method is part of an object, I don't think it will work directly (because of C++'s implicit "this" argument to member functions.)
A possible soloution is a non-member wrapper around this function, like so: