我有下面的代码片段,一个试图将对set(foo)中的对象的引用传递给另一个函数(goo)的函数.
/**************** working ***************/
{
std::set<MyStruct> foo;
MyStruct abc = *foo.begin();
goo(abc);
}
int goo(MyStruct & st)
{
//some code here
}
/**************** not working ***************/
{
std::set<MyStruct> foo;
goo( *(foo.begin()) );
}
int goo(MyStruct & st)
{
//some code here
}
Run Code Online (Sandbox Code Playgroud)
和错误列表:
>file.cpp:190:45: error: no matching function for call to 'goo(const MyStruct&)'
>file.cpp:190:45: note: candidate is:
>file.h:33:9: note: int goo(MyStruct&)
>file.h:33:9: note: no known conversion for argument 1 from 'const MyStruct' to 'MyStruct&'
Run Code Online (Sandbox Code Playgroud)
第一个版本编译正常,而第二个版本报告错误.我已经看过const关键字,但我不是在任何地方将const括起来的,而且这个集合不是const.你能解释一下我做错了什么吗?