0

我有这个模板类:

#include <utility>
#include <boost/locale.hpp>
#include <boost/range/iterator.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/range/algorithm_ext/insert.hpp>
#include <boost/icl/interval_set.hpp>
#include <boost/container/flat_set.hpp>
#include <boost/container/list.hpp>
#include <boost/container/vector.hpp>
#include <boost/container/string.hpp>
#include <boost/foreach.hpp>

template<typename IntType>
class IntervalSet {
private:
    boost::icl::interval_set<IntType> impl;
    bool readonly;

    bool doIchangeReadOnly() {
        if(readonly){
            // ERROR MESSAGE
        }

        return false;
    }
public:
    IntervalSet(const boost::icl::interval_set<IntType> &c) : impl(c), readonly(false) {}
    IntervalSet(){}

    static const IntervalSet EMPTY_SET;

    static IntervalSet of(IntType a) {
        IntervalSet r;
        r.add(a);
        return r;
    }

    // Create a set with all ints within range [a..b] (inclusive)
    static IntervalSet of(IntType a, IntType b) {
        IntervalSet r;
        r.add(a, b);
        return r;
    }

    // Add element el to the set.
    void add(IntType el) {
        if(doIchangeReadOnly()) return;

        impl += el;
    }

    // Add interval; i.e., add all integers from a to b to set.
    //  If b<a, do nothing.
    //  Keep list in sorted order (by left range value).
    //  If overlap, combine ranges.  For example,
    //  If this is {1..5, 10..20}, adding 6..7 yields
    //  {1..5, 6..7, 10..20}.  Adding 4..8 yields {1..8, 10..20}.
    void add(IntType a, IntType b) {
        if(doIchangeReadOnly() || b<a) return;

        impl.add(boost::icl::interval<int>::type(a, b));
    }

    const boost::icl::interval_set<IntType>& data() const{
        return impl;
    }

    void clear() {
        if(doIchangeReadOnly()) return;

        impl.clear();
    }

    // Remove element el from the set.
    void remove(IntType el) {
        if(doIchangeReadOnly()) return;

        impl -= el;
    }

    // Add all elements from incoming set to this set.
    // Return "this" so we can chain calls.
    IntervalSet<IntType>& addAll(const IntervalSet<IntType> &set) {
        if(doIchangeReadOnly()) return *this;

        impl += set.impl;
        return *this;
    }

    // Return the intersection of this set with the argument, creating a new set.
    IntervalSet<IntType> and(IntervalSet<IntType> a) const {
        a.impl &= impl;
        return a;
    }
};

template<typename IntType>
const IntervalSet<IntType> IntervalSet<IntType>::EMPTY_SET = IntervalSet<IntType<();

当我删除最后一个方法定义(IntervalSet<IntType>(IntervalSet<IntType> a) const)时,g++ 只编译我的源代码。

我的问题:我到底做错了什么?

来自 g++ 的错误消息:

 \m.cpp     137     error: expected ')' before 'a'
 \m.cpp     137     error: 'a' does not name a type
4

3 回答 3

1

至少一个错误是:

IntervalSet<IntType<();
                   ^^?

应该:

 IntervalSet<IntType>();
于 2013-02-14T00:49:36.807 回答
0

现在我知道了错误:

该代码无法编译,因为 and 被 gcc 视为关键字:

改变

IntervalSet<IntType> and(IntervalSet<IntType> a) const {

IntervalSet<IntType> AND(IntervalSet<IntType> a) const

它工作正常。

于 2013-02-14T08:00:59.497 回答
0

It looks like you are defining a template function in a cpp file.

Template functions must be defined and implemented in header files. There are several questions concerning this on Stack Overflow, e.g. Why can templates only be implemented in the header file?

于 2013-02-14T01:05:26.127 回答