g ++不编译我的源代码。也许有些模板错误?

时间:2013-02-14 00:47:23

标签: c++

我有这个模板类:

#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<();

g ++仅在我删除最后一个方法定义(IntervalSet<IntType>(IntervalSet<IntType> a) const)时编译我的来源。

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

来自g ++的错误消息:

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

3 个答案:

答案 0 :(得分:1)

至少有一个错误:

IntervalSet<IntType<();
                   ^^?

应该是:

 IntervalSet<IntType>();

答案 1 :(得分:0)

看起来您正在cpp文件中定义模板函数。

必须在头文件中定义和实现模板函数。 Stack Overflow上有几个与此有关的问题,例如: Why can templates only be implemented in the header file?

答案 2 :(得分:0)

现在我知道了错误:

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

更改

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

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

它工作正常。