1

I follow your suggests by now I have another problem.

ifndef MARKERCONTAINE_H
define MARKERCONTAINE_H
include <list>
include <string>
include "Marker.h"
namespace Ableton {

    struct DeferencedEqual{

        DeferencedEqual(const Marker* compare_to) : compare_to_(compare_to) { }

        bool operator()(Marker* c1) const {return  *c1 == *compare_to_;}  

        private:
            const Marker* compare_to_;
    };

    bool compare(Marker* const p1, Marker* const p2){return p1<p2;}

class MarkerContainer {

private:
    std::list<Marker*> list;
    double end_t;
    inline bool exists(std::list<Marker*> *input_list, Marker* check);
   // inline int computeInterval(double to_convert, bool beat_t=true);

public:
    MarkerContainer();
    MarkerContainer(double end_time);
    bool addMarker(Marker* input);
    double computeBeatTime(double sample_t);
    double computeSampleTime(double beat_t);    
    void printAll();
    void sort();

};



}

The function sort is now :

 namespace Ableton {

    void MarkerContainer::sort(){

       list.sort(compare);
    }

    }

And my main.cpp is :

include "Marker.h"
include "MarkerContainer.h"
include <iostream>
include <list>
include <string>
include <cctype>

using namespace std;

int main (int argc, const char * argv[])
{

    Ableton::MarkerContainer::MarkerContainer container =  Ableton::MarkerContainer::MarkerContainer(10.0);
    Ableton::Marker::Marker* one = new Ableton::Marker::Marker(1.0, 1.0);
    Ableton::Marker::Marker* two = new Ableton::Marker::Marker(2.0, 1.0);
    Ableton::Marker::Marker* three = new Ableton::Marker::Marker(3.0, 1.0);

    if(!container.addMarker(one))
        cout << *one << "NOT Added" << endl;
    if(!container.addMarker(two))
        cout << *two << "NOT Added" << endl;
    if(!container.addMarker(three))
        cout << *three << "NOT Added" << endl;
    cout << "-- LIST " << endl;

    container.printAll();

    cout << "-- LIST after sort" << endl;
    //container.printAll();

}

Now I catch this ld error, it sounds strange for me... no way for me in C++ ? :-)

ld: duplicate symbol Ableton::compare(Ableton::Marker*, Ableton::Marker*) in /..../MarkerContainer.o and /..../main.o for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) Command /Developer/usr/bin/clang++ failed with exit code 1

Thanks again in advance, do you can suggest me some book regarding STL and how it works "underground", in order to understand better what happened in these cases technically speacking

pedr0

pedr0
  • 2,941
  • 6
  • 32
  • 46

2 Answers2

2

The problem is perhpas that you have included the definition for:

Ableton::compare(Ableton::Marker*, Ableton::Marker*)    

in a header file and the header file gets included multiple times leading to multiple definitions of the compare function and the linker complains because having such multiple definitions break the One Definition Rule.

Solution:
Putt the definition in a cpp file or mark it as inline in case you want it to be in the header file.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • But I implemented this rule : http://en.wikipedia.org/wiki/Include_guard, why it does not work ? – pedr0 Feb 05 '12 at 12:51
  • @pedr0: Include guards guard against multiple inclusion in a single translation unit. `inline` allows for multiple definitions across many translation units. – Mankarse Feb 05 '12 at 12:55
  • @pedr0: This answers your Question, [Why aren't my compile guards preventing multiple definition inclusions?](http://stackoverflow.com/questions/249701/why-arent-my-compile-guards-preventing-multiple-definition-inclusions) – Alok Save Feb 05 '12 at 12:56
0

I think you should use references in the comparator. Try this variant

bool compare(const Marker*&  p1, const Marker*&  p2){return p1<p2;}
mikithskegg
  • 806
  • 6
  • 10