与LEDA库的C ++比较

时间:2009-08-21 22:42:29

标签: c++ leda

我正在尝试使用LEDA库创建一个Set ...我正在添加一个Class的元素,该类在命名空间LEDA下定义了compare()方法...不幸的是,编译器无法找到比较这个类的函数......这是错误信息......

/home/user/Desktop/leda/incl/LEDA/core/set.h: 
In constructor ‘leda::set<E, set_impl>::set() [with E = Operator*, set_impl = leda::avl_tree]’: 
../src/suite.cc:52:   instantiated from here /home/user/Desktop/leda/incl/LEDA/core/set.h:71: error: no matches converting function ‘compare’ to type ‘int (*)(class Operator* const&, class Operator* const&)’ 
    /home/user/Desktop/leda/incl/LEDA/internal/param_types.h:351: error: candidates are: int leda::compare(const char&, const char&) 
    /home/user/Desktop/leda/incl/LEDA/internal/param_types.h:352: error:                 int leda::compare(const unsigned char&, const unsigned char&) 
    /home/user/Desktop/leda/incl/LEDA/internal/param_types.h:353: error:                 int leda::compare(const int&, const int&) 
    /home/user/Desktop/leda/incl/LEDA/internal/param_types.h:354: error:                 int leda::compare(const unsigned int&, const unsigned int&) 
    /home/user/Desktop/leda/incl/LEDA/internal/param_types.h:355: error:                 int leda::compare(const long int&, const long int&) 
    /home/user/Desktop/leda/incl/LEDA/internal/param_types.h:356: error:                 int leda::compare(const long unsigned int&, const long unsigned int&) 
    /home/user/Desktop/leda/incl/LEDA/internal/param_types.h:357: error:                 int leda::compare(const float&, const float&) 
    /home/user/Desktop/leda/incl/LEDA/internal/param_types.h:358: error:                 int leda::compare(const double&, const double&) 
    /home/user/Desktop/leda/incl/LEDA/core/string.h:382: error:                 int leda::compare(const leda::string&, const leda::string&)

LEDA要求为compare()的元素定义set方法。

Suite::Suite (set<Operator*> *ops)
  : operators(ops!=NULL ? ops : new set<Operator*>)
{

我根据LEDA要求定义了这种比较方法......

namespace leda {
inline int compare (Operator* const &a, Operator* const &b)
{
  return a==b ? 0 : a<b ? -1 : 1;
}
};

但它仍然无法在LEDA librabry的set.h中找到它。

 set() { cmp_ptr = compare; }

它试图找到指向compare()方法的指针并将其分配给cmp_ptr ...但找不到它......

我已经定义了这个方法,但不知何故它没有得到识别?

更新:我似乎遇到了所有compare()定义的问题....包括集合中的其他类..

2 个答案:

答案 0 :(得分:0)

附加一个更简单的例子来重现相同的错误...... 您需要为此免费下载LEDA库的免费版本..但使用起来很轻松...... http://www.algorithmic-solutions.com/leda/ledak/index.htm

这是头文件... pair.h

#include <istream>
#include <LEDA/core/set.h>

using leda::set;

class pair {
private:
      int  x;
      int  y;

public:
    pair() { x = y = 0; }
    pair(const pair& p) { x = p.x; y = p.y; }
    pair& operator=(const pair& p) {
        if(this != &p) { x = p.x; y = p.y; }
        return *this;
    }

    friend std::istream& operator>> (std::istream& is, pair& p) 
    { is >> p.x >> p.y; return is; }

    friend std::ostream& operator<< (std::ostream& os, const pair& p) 
    { os << p.x << " " << p.y; return os; }

    pair(int a, int b) { x = a;  y = b; }

    int get_x() const { return x; }
    int get_y() const { return y; }
};

namespace leda {
    inline int compare(const pair& p, const pair& q)
    {
        if (p.get_x() < q.get_x()) return  -1;
        if (p.get_x() > q.get_x()) return   1; 
        if (p.get_y() < q.get_y()) return  -1;
        if (p.get_y() > q.get_y()) return   1;
        return 0;
    }
};

这是主要的c ++文件pair.cc

#include<pair.h>

using std::cout;
using std::endl;

main() {

    set<pair> T;
    cout<<"Hello world"<<endl;
}

只需使用以下命令编译它们:

g++ -I$LEDA_INCLUDE_PATH -I$PATH_TO_PAIR.H pair.cc

答案 1 :(得分:0)

好的,这个bug很奇怪!必须在包含LEDA标头之前定义compare()方法....