【问题标题】:Why is this binary apply_visitor not working?为什么这个二进制 apply_visitor 不起作用?
【发布时间】:2013-03-07 11:53:16
【问题描述】:
#include <string>
#include "boost/variant/variant.hpp"
#include "boost/variant/apply_visitor.hpp"

using namespace std;

class Base
{
public:
    Base(){}
    ~Base(){}
    void AddField(int tag, int value){std::cout << "Base::AddField " << tag << ", " << value << std::endl;}
    void AddField(int tag, string value){std::cout << "Base::AddField " << tag << ", " << value << std::endl;}
};

class A : public Base
{
public:
    A(){}
    ~A(){}
};
class B : public Base
{
public:
    B(){}
    ~B(){}
};

class foo_visitor
    : public boost::static_visitor<>
{
public:
    foo_visitor(int tag){mTag = tag;}
    template <typename T>
    void operator()(T &a, int &v) const {
        a->AddField(mTag, v);
    }
private:
    int mTag;
};

int main(int argc, char **argv) 
{
    typedef boost::variant<A*,B*> AB;
    AB ab = new A();    
    int tag = 1;
    int v = 2;
    boost::apply_visitor(foo_visitor(tag), ab, v);
    return 0;
}

我得到这个编译错误:

apply_visitor_unary.hpp:60:43: error: request for member ‘apply_visitor’ in ‘visitable’, 这是 非类类型‘int’

我的代码有什么问题?

【问题讨论】:

    标签: c++ boost apply-visitor


    【解决方案1】:

    int 真的不是variant。

    接受两个操作数的重载调用二进制函数调用 给定访问者对给定变体内容的操作员 操作数。

    http://www.boost.org/doc/libs/1_52_0/doc/html/boost/apply_visitor.html

    【讨论】:

    • @llonesmiz 是的。而已。请将其发布为答案。
    • @Meysam 我只是听从了这个答案中的建议。
    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    相关资源
    最近更新 更多