I did not understand really well how Predicate works, then my question could be stupid or bad written.
I wrote this class :
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_;
};
struct DeferencedCompare{
bool operator()(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();
};
}
And this is my .cpp class implementation (just an section in which I am interesting in):
namespace Ableton {
void MarkerContainer::sort(){
std::sort(list.begin(), list.end(),DeferencedCompare());
}
}
I had got this compiler error:
/usr/include/c++/4.2.1/bits/stl_algo.h:2867:22:{2867:15-2867:21}{2867:24-2867:31}: error: invalid operands to binary expression (‘std::_List_iterator’ and ‘std::_List_iterator’) [2]
Any ideas about ?
Thanks in advance.
Edit One
Hi, I follow your suggests by now I have another problem.
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
Sure: you can’t use
std::sort()to sort astd::list<T>becausestd::sort()requires random access iterators. However,std::list<T>only supports bidrectional iterators. You can usestd::list<T>::sort(), however.