c++ lambda 表达式帮助
我对 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
和 memory_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,在 C++ 中,您不需要(这通常被认为是不好的风格)将 struct 放在结构的使用前面。只需
const memory_leak_report&
就可以了。其次,您告诉我们 struct
memory_allocation_record
是如何定义的,但是 lambda 采用memory_leak_report
作为其参数,据我所知,它是一个功能。那是你的错误吗? lambda 是否应该采用
memory_allocation_record
来代替?当然,这引出了最后一点。如果您遇到错误,您是否认为有必要告诉我们该错误是什么?否则,我们必须猜测代码中可能的问题。
编辑
好吧,正如我所怀疑的,这似乎是问题所在。我可以建议实际阅读编译器的错误。这就是他们在那里的原因。 ;)
取错误的第一行:
去掉不相关的位:
现在,因为这是一个 lambda,所以类型有点复杂,但最终,它是在谈论函数调用,因此最后一个括号描述了参数。换句话说,它尝试调用以
memory_allocation_record&
作为参数的函数,但无法找到匹配的函数。相反,它找到了第二行中描述的候选者:
因此,它实际上找到的候选者采用 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. Justconst memory_leak_report&
would do fine.Second, you tell us how the struct
memory_allocation_record
is defined, but the lambda takes amemory_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:
strip away the irrelevant bits:
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:
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 aconst memory_leak_report&
?