C++ 自定义类没有可行的重载运算符[]

C++ 自定义类没有可行的重载运算符[],c++,class,vector,C++,Class,Vector,我已经编写了std::vector的自定义子类: template <class T> class CustVec : public vector<vector<T>> { public: T& operator [](const pair<int, int> pos) { return (*this)[pos.first][pos.second]; } }; 模板 类CustVec:公共向量{ 公众:

我已经编写了std::vector的自定义子类:

template <class T>
class CustVec : public vector<vector<T>> {

public:

    T& operator [](const pair<int, int> pos) {
        return (*this)[pos.first][pos.second];
    }
};
模板
类CustVec:公共向量{
公众:
T&operator[](常量对位置){
返回(*此)[第一位][第二位];
}
};

但是对于类型“CustVec”“,我遇到了一个错误,
没有可行的重载运算符[]。如何修复它?

您通过声明一个新运算符来隐藏基类
运算符[]
。通常,这会从外部范围隐藏类似的名称

通过显式导入名称,可以使其再次可见

using vector<vector<T>>::operator[];
使用向量::运算符[];
也许把它放在私人部分,为了不让它公开

template <class T>
class CustVec : public vector<vector<T>> {

    using vector<vector<T>>::operator[];

public:

    T& operator [](const pair<int, int> pos) {
        return (*this)[pos.first][pos.second];
    }
};
模板
类CustVec:公共向量{
使用向量::运算符[];
公众:
T&operator[](常量对位置){
返回(*此)[第一位][第二位];
}
};

您使用什么编译器?不要从标准容器继承。而是使用组合。问题是
(*this)[pos.first]
看起来像一个递归调用,但参数错误。@Valentin,Xcode 7.1,但问题也出现在其他编译器上。