3
#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: 错误:在“visitable”中请求成员“apply_visitor”,它是非类类型“int”</p>

我的代码有什么问题?

4

1 回答 1

3

int不是variant真的。

接受两个操作数的重载在给定变体操作数的内容上调用给定访问者的二进制函数调用运算符。

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

于 2013-03-07T12:05:36.787 回答