小编Evi*_*ael的帖子

C++无法从派生类中调用基类方法

这是我的第一个问题,我希望我做的一切都是正确的.

我尝试从boost元组派生一个类.Boost的元组提供了一个get()模板方法来访问各个字段.有趣的是,我不能在派生类中使用该方法.

以下代码显示了问题:

#include <iostream>
#include <boost/tuple/tuple.hpp>
using namespace std;

template<typename A>
class Derived : public boost::tuple<A>
{
public:
    Derived() : boost::tuple<A>() {}

    A& getVal0()
    {
        return get<0>();
        // does not compile:
        //error: 'get' was not declared in this scope

        return boost::tuple<A>::get<0>();
        // does not compile
        //error: expected primary-expression before ')' token

        return boost::tuples::get<0>(*this);
        //works
    }
};  

int main() {
    Derived<int> a;

    a.get<0>() = 5;

    cout << a.get<0>() << endl; 
    cout << a.getVal0() << endl; 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想知道为什么我可以get<0>() …

c++ boost boost-tuples

7
推荐指数
1
解决办法
365
查看次数

标签 统计

boost ×1

boost-tuples ×1

c++ ×1