C ++模板类的重载[]运算符

时间:2012-07-21 21:02:57

标签: c++ templates map operator-overloading

我想根据模板参数重载模板类的[]运算符。像这样:

template<
    typename T,
    template<typename> class Property, 
    template<typename> class Key1, 
    template<typename> class Key2>
class a_map 
{
public:
    const Property<T>& operator[](const Key1<T>& k) const
    { return _values[k.index()]; }
    const Property<T>& operator[](const Key2<T>& k) const
    { return _values[k.index()]; }
protected:
    std::vector<Property<T> > _values;
};

我会像这样使用这个类:

int main()
{
    a_map<float, prop, key_a, key_b> pm;
}

基本上我希望能够访问_values向量中的元素,而不必担心Key类型。重要的是他们有一个index()成员。

但是我收到以下错误

  

错误C2535:'const Property&amp; a_map :: operator   [](const Key1&amp;)const':已定义的成员函数或   声明

即使key_akey_b是两个完全不同的类型类模板。

我错过了什么吗?编译器是否担心在某些情况下Key1<T>Key2<T>实际上可能是同一类型?

修改 这些是main

中使用的类模板
template<typename T>
struct prop 
{
    T weight;
    T height;
};


template<typename T>
class key_a
{
public:
    int index() { return _i; }
private:
    int _i;
};

template<typename T>
class key_b
{
public:
    int index() { return 3; } // Always return 3

修改 我正在使用MVC ++ 2008编译器。

3 个答案:

答案 0 :(得分:1)

由于你的operator []都是相同的,除了参数类型,为什么不模拟它们?

    template <typename TT>
    const Property<T>& operator[](const TT& k) const
    {
        return _values[k.index()];
    }

答案 1 :(得分:1)

您需要将index()函数声明为const,因为在a_map模板中您通过const个对象调用它们

template<typename T> class key_a {
public:
    int index() const // <- `const` is necessary
      { return _i; }
private:
    int _i;
};

template<typename T> class key_b {
public:
    int index() const // <- `const` is necessary
      { return 3; } 
};

但是否则,一切都会编译并且对我来说很好。


在VS2010编译器中尝试并获得与您相同的错误。这显然是MSVC ++编译器中的编译器错误。模板模板参数的处理实现不正确。

要解决这个问题,我可以使用这种技术

template<
    typename T,
    template<typename> class Property, 
    template<typename> class Key1, 
    template<typename> class Key2>
class a_map 
{
public:
    const Property<T>& operator[](const typename Key1<T>::self& k) const
    { return _values[k.index()]; }
    const Property<T>& operator[](const typename Key2<T>::self& k) const
    { return _values[k.index()]; }
protected:
    std::vector<Property<T> > _values;
};

并将关键模板定义为

template<typename T> class key_a {
public:
    typedef key_a self;
    int index() const { return _i; }
private:
    int _i;
};

template<typename T> class key_b {
public:
    typedef key_b self;
    int index() const { return 3; } 
};

这是不优雅的,但它使它可以正常使用MSVC ++编译器。

答案 2 :(得分:0)

像这样编译好......注意应该如何定义Prop / K1 / K2。

#include <vector>

template<
    typename T,
    template<typename> class Property, 
    template<typename> class Key1, 
    template<typename> class Key2>
class a_map 
{
public:
    const Property<T>& operator[](const Key1<T>& k) const
    { return _values[k.index()]; }
    const Property<T>& operator[](const Key2<T>& k) const
    { return _values[k.index()]; }
protected:
    std::vector<Property<T> > _values;
};

template <typename T> struct K1 { int index() const { return 0; } };
template <typename T> struct K2 { int index() const { return 0; } };
template <typename T> struct Prop { };

int main()
{
    a_map<float, Prop, K1, K2> m;
}
相关问题