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>g ++は、最後のメソッド定義(および)を削除したときにのみソースをコンパイルします(IntervalSet<IntType> a) const

私の質問:一体何が間違っているのですか?

g ++からのエラーメッセージ:

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

3 に答える 3

1

少なくとも1つのエラーは次のとおりです。

IntervalSet<IntType<();
                   ^^?

する必要があります:

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

cpp ファイルでテンプレート関数を定義しているようです。

テンプレート関数は、ヘッダー ファイルで定義および実装する必要があります。Stack Overflow では、これに関するいくつかの質問があります。たとえば、テンプレートをヘッダー ファイルにしか実装できないのはなぜですか?

于 2013-02-14T01:05:26.127 に答える
0

今、私はエラーを知っています:

and は gcc によってキーワードと見なされるため、コードはコンパイルされません。

変化する

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

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

そしてそれはうまくいきます。

于 2013-02-14T08:00:59.497 に答える