pedr0
pedr0

Reputation: 3011

Overload of std:find function

I tried to overload the std::find function, in this way:

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

I hope I did some stupid mistake and what I would like to do is simple.. any idea ?

Thanks in advance! Pietro

Upvotes: 0

Views: 1987

Answers (2)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153792

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. Specifically with namespace std I don't think you are allowed to do this. 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:

auto it = std::find_if(list.begin(), list.end(),
                       [=](Abelton::Marker* el)->bool { return *el == *check; });

Upvotes: 0

Mark B
Mark B

Reputation: 96233

You aren't allowed to overload 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.

// 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));

Upvotes: 3

Related Questions