使用 for_each 时出错

发布于 2024-10-31 13:42:25 字数 598 浏览 0 评论 0原文

我的代码中有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

脸赞 2024-11-07 13:42:25

for_each 算法接收 一元函数 应具有以下签名:

void function(T&);

其中 T 是 g1.Vertice1 向量的元素类型:

template <class VertexType, class EdgeType>
void num(project3::Vertex<VertexType, EdgeType>* v) {
  *v = 0; // <- Maybe v->set(0,0,0)
}

for_each algorithm receives a unary function which should have the following signature:

void function(T&);

where T is the type of element of g1.Vertice1 vector:

template <class VertexType, class EdgeType>
void num(project3::Vertex<VertexType, EdgeType>* v) {
  *v = 0; // <- Maybe v->set(0,0,0)
}
呆° 2024-11-07 13:42:25

摘自SGI 网站

For_each 将函数对象 f 应用于范围 [first, last) 中的每个元素; f 的返回值(如果有)将被忽略。

一元函数是一种函数对象:像普通 C++ 函数一样调用的对象。使用单个参数调用一元函数。

据我从文档中了解到, for_each 构造将迭代传递的集合并将当前元素传递给您的函数,然后您需要相应地更新传递的项目。

Taken from the SGI site:

For_each applies the function object f to each element in the range [first, last); f's return value, if any, is ignored.

And

A Unary Function is a kind of function object: an object that is called as if it were an ordinary C++ function. A Unary Function is called with a single argument.

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.

杯别 2024-11-07 13:42:25

根据我之前对同一程序/同一用户的经验,我会说 num 需要采用字符串参数,因为它需要进行操作(查看 std::for_each 的第三个参数的声明)标头中的 () 方法)。

另外,如果 num() 方法是对象的一部分,我认为它不会直接工作(因为 C++ 对成员函数的隐式“this”参数。)

一个可能的解决方案是围绕此函数的非成员包装器,像这样:

void my_non_member(string str)
{
    myobj.my_member(str); // myobj is a reference to a Vertice object that must be  
    // initialised elsewhere.
}

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:

void my_non_member(string str)
{
    myobj.my_member(str); // myobj is a reference to a Vertice object that must be  
    // initialised elsewhere.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文