c++ lambda 表达式帮助

发布于 2024-11-28 05:31:25 字数 1312 浏览 1 评论 0原文

我对 c++0x 有点陌生,任何人都可以向我解释为什么以下内容无法编译:

void memory_leak_report()
{
    std::cout.flags(std::ios::showbase);
    std::for_each(allocation_records.begin(), allocation_records.end(),
                  [] (const struct memory_leak_report& rec) {
                      std::cout << "memory leak at: " << rec.file << ": line " << rec.line
                                << std::hex << ", address: " << rec.address << std::dec
                                << ", size:" << rec.size << std::endl;
    });
}

其中 allocate_records 定义为: std::list; Allocation_recordsmemory_allocation_record 是一个简单的 C 风格数据结构。

struct memory_allocation_record {
    const char *func;
    const char *file;
    unsigned int line;
    unsigned int size;
    unsigned long address;
};

我尝试使用以下命令编译它: g++ -Wall -g -o alloc main.cpp -std=c++0x

我得到的错误是: 在函数 ג_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = std::_List_iterator, _Funct = memory_leak_report()::]

错误:与 (memory_leak_report()::) (memory_allocation_record&) 调用不

匹配:候选者是:void (*)(const memory_leak_report()::memory_leak_report&)

I'm a bit new to c++0x, can anyone explain to me why the following fail to compile:

void memory_leak_report()
{
    std::cout.flags(std::ios::showbase);
    std::for_each(allocation_records.begin(), allocation_records.end(),
                  [] (const struct memory_leak_report& rec) {
                      std::cout << "memory leak at: " << rec.file << ": line " << rec.line
                                << std::hex << ", address: " << rec.address << std::dec
                                << ", size:" << rec.size << std::endl;
    });
}

where allocation_records is defined as: std::list<struct memory_allocation_record> allocation_records and memory_allocation_record is a simple C style data structure.

struct memory_allocation_record {
    const char *func;
    const char *file;
    unsigned int line;
    unsigned int size;
    unsigned long address;
};

I've tried compiling it with: g++ -Wall -g -o alloc main.cpp -std=c++0x

The errors I get are:
In function ג_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = std::_List_iterator, _Funct = memory_leak_report()::]

error: no match for call to (memory_leak_report()::) (memory_allocation_record&)

note: candidates are: void (*)(const memory_leak_report()::memory_leak_report&)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

萌︼了一个春 2024-12-05 05:31:25

首先,在 C++ 中,您不需要(这通常被认为是不好的风格)将 struct 放在结构的使用前面。只需 const memory_leak_report& 就可以了。

其次,您告诉我们 struct memory_allocation_record 是如何定义的,但是 lambda 采用 memory_leak_report 作为其参数,据我所知,它是一个功能。

那是你的错误吗? lambda 是否应该采用 memory_allocation_record 来代替?

当然,这引出了最后一点。如果您遇到错误,您是否认为有必要告诉我们该错误是什么?否则,我们必须猜测代码中可能的问题。

编辑
好吧,正如我所怀疑的,这似乎是问题所在。我可以建议实际阅读编译器的错误。这就是他们在那里的原因。 ;)

取错误的第一行:

/usr/include/c++/4.5/bits/stl_algo.h:4185:2: error: no match for call to ג(memory_leak_report()::<lambda(const memory_leak_report()::memory_leak_report&)>) (memory_allocation_record&)

去掉不相关的位:

no match for call to <somethingwithlambdas> (memory_allocation_record&)

现在,因为这是一个 lambda,所以类型有点复杂,但最终,它是在谈论函数调用,因此最后一个括号描述了参数。换句话说,它尝试调用以 memory_allocation_record& 作为参数的函数,但无法找到匹配的函数。

相反,它找到了第二行中描述的候选者:

candidates are: void (*)(const memory_leak_report()::memory_leak_report&) <conversion>

因此,它实际上找到的候选者采用 const memory_leak_report& 作为其参数。

现在您只需比较两者即可。当编译器尝试将 memory_allocation_record& 传递给需要 const memory_leak_report& 的函数时,这意味着什么?

First, In C++, you don't need to (and it's usually considered bad style) put struct in front of uses of a struct. Just const memory_leak_report& would do fine.

Second, you tell us how the struct memory_allocation_record is defined, but the lambda takes a memory_leak_report as its parameter, which, as far as I can see, is a function.

is that your error? Was the lambda supposed to take a memory_allocation_record instead?

Which brings us to the last point, of course. If you get an error, don't you think it'd be relevant to tell us what that error is? Otherwise, we have to guess at what we think might be a problem in your code.

Edit
Ok, as I suspected, that seems to be the problem. I can recommend actually reading the compiler's errors. That's why they're there. ;)

Take the first line of the error:

/usr/include/c++/4.5/bits/stl_algo.h:4185:2: error: no match for call to ג(memory_leak_report()::<lambda(const memory_leak_report()::memory_leak_report&)>) (memory_allocation_record&)

strip away the irrelevant bits:

no match for call to <somethingwithlambdas> (memory_allocation_record&)

Now, because this is a lambda, the type is a bit hairy, but ultimately, it's talking about a function call, and so the last parentheses describe the parameter. In other words, it tries to call a function with a memory_allocation_record& as its parameter, but is unable to find a matching function.

Instead, it found the candidate described in the second line:

candidates are: void (*)(const memory_leak_report()::memory_leak_report&) <conversion>

So, the candidate it actually found takes a const memory_leak_report& as its parameter.

Now you just need to compare the two. What could it mean, when the compiler tries to pass a memory_allocation_record& to a function that expects a const memory_leak_report&?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文