2012-02-05 61 views
1

我按照你的建议,现在我有另一个问题。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

+0

你能指出上面的代码片段的源文件吗? – hmjd 2012-02-05 11:50:01

回答

2

的问题是,您已经为定义perhpas:

Ableton::compare(Ableton::Marker*, Ableton::Marker*)  
在头文件和头文件

被多次包括导致比较功能的多个定义和链接器抱怨,因为有这样的多个定义打破了One Definition Rule

解决方案:
推杆在cpp文件的定义或将其标记为inline如果你希望它是在头文件。

+0

但我实现了这个规则:http://en.wikipedia.org/wiki/Include_guard,为什么它不起作用? – pedr0 2012-02-05 12:51:05

+0

@ pedr0:包括警卫在一个翻译单元中防止多重包含。 “内联”允许跨多个翻译单元进行多种定义。 – Mankarse 2012-02-05 12:55:34

+0

@ pedr0:这回答你的问题,[为什么不是我的编译警卫防止多重定义包含?](http://stackoverflow.com/questions/249701/why-arent-my-compile-guards-preventing-multiple-定义夹杂物) – 2012-02-05 12:56:46

0

我想你应该在比较器中使用引用。试试这个变种

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