[英]Overload of std:find function
I tried to overload the std::find function, in this way: 我试图以这种方式重载std :: find函数:
include <list>
include <string>
include "Marker.h"
namespace Test {
class MarkerContainer {
private:
std::list<Marker*> list;
double end_t;
inline bool exists(std::list<Marker*> *input_list, Marker* check);
public:
MarkerContainer();
MarkerContainer(double end_time);
bool addMarker(Marker* input);
double computeBeatTime(double sample_t);
double computeSampleTime(double beat_t);
void printAll();
};
}
std::list<Ableton::Marker*>::iterator std::find(std::list<Ableton::Marker*>::iterator first, std::list<Ableton::Marker*>::iterator last, Ableton::Marker* value){
for ( ;first!=last; first++) if ( **first==*value ) break;
return first;
}
But I catch this compiler error: 但是我遇到了这个编译器错误:
Out-of-line definition of 'find' does not match any declaration in namespace 'std' in /Users/.../MarkerContainer.h 'find'的脱机定义与/Users/.../MarkerContainer.h中名称空间'std'中的任何声明都不匹配
I hope I did some stupid mistake and what I would like to do is simple.. any idea ? 我希望我犯了一些愚蠢的错误,我想做的就是简单..任何想法吗?
Thanks in advance! 提前致谢! Pietro 彼得罗
You aren't allowed to overload find
. 您不允许重载find
。 You are allowed to specialize templates in the std
namespace, but in this case it's far simpler to use find_if
with a dereferencing functor. 您可以在std
名称空间中专门化模板,但是在这种情况下,将find_if
与解引用函子一起使用要简单得多。
// Create DerefEquality comparison (I'll try to come back later and write it).
container_type::iterator iter = find_if(container.begin(), container.end(), DerefEquality(item_to_find));
You can't overload a function like this: you need to define the overload in an appropriate namespace. 您不能重载这样的函数:您需要在适当的名称空间中定义重载。 You need to open namespace std
at global scope. 您需要在全局范围内打开命名空间std
。 Specifically with namespace std
I don't think you are allowed to do this. 特别是对于命名空间std
我认为您不允许这样做。 Otherwise this would work. 否则,它将起作用。
That said, I think you should look at using std::find_if()
with a suitable predicate instead of using your hand-crafted approach: 也就是说,我认为您应该考虑将std::find_if()
与合适的谓词一起使用,而不是使用手工制作的方法:
auto it = std::find_if(list.begin(), list.end(),
[=](Abelton::Marker* el)->bool { return *el == *check; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.