我的代码中有以下两个函数:
bool num()
{
return 0;
}
void setDFS()
{
int i = 0;
project3::Graph<string, string> g1;
std::for_each(g1.Vertice1.begin(), g1.Vertice1.end(),num);
}
该函数所做的是针对 vector Vertice1 中的每个顶点,现在必须将其编号设置为 0。一旦我开始图形遍历,稍后我会将 num 递增到遍历的计数。
编译时,我得到“ error C2197: 'bool (__cdecl *)(void)' : 调用参数过多”错误。
template <class VertexType, class EdgeType> class Vertex{
protected:
VertexType vertice;
EdgeType edge;
public:
};
std::vector<project3::Vertex<VertexType, EdgeType>*> Vertice1;
最佳答案
for_each算法收到 unary function它应该具有以下签名:
void function(T&);
其中 T 是 g1.Vertice1 vector 的元素类型:
template <class VertexType, class EdgeType>
void num(project3::Vertex<VertexType, EdgeType>* v) {
*v = 0; // <- Maybe v->set(0,0,0)
}
关于c++ - 使用 for_each 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5612210/