我按照你的建議,現在我有另一個問題。std :: list.sort(Predicate)編譯錯誤
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();
};
}
功能排序是現在:
namespace Ableton {
void MarkerContainer::sort(){
list.sort(compare);
}
}
而我的main.cpp是:
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();
}
現在我抓住這個LD的錯誤,這對我來說聽起來很奇怪......不我在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
再次感謝提前,你可以建議我一些書關於STL和它是如何工作的「地下」,爲了更好地瞭解在這些情況下發生了什麼技術上speacking
pedr0
你能指出上面的代碼片段的源文件嗎? – hmjd 2012-02-05 11:50:01