c++ - 为什么这个二进制 apply_visitor 不工作?

标签 c++ boost apply-visitor

#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’, which is of non-class type ‘int’

我的代码有什么问题?

最佳答案

int 实际上不是 variant

Overloads accepting two operands invoke the binary function call operator of the given visitor on the content of the given variant operands.

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

关于c++ - 为什么这个二进制 apply_visitor 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15270512/

相关文章:

c++ - boost::apply_visitor 的返回类型是什么(延迟版本)

c++ - 通过 boost::asio::udp 以高数据速率发送图像数据?

c++ - 通过 boost::python 将 C++ 对象传递给 python 函数

c++ - 2 个派生类的虚函数(十六进制 + 二进制)

c++ - C out of header in C++

c++ - 使用 phoenix 构造时调用析构函数

c++ - 无法将 boost 安装到其他目​​录

c++ - CGAL::交集错误

c++ - boost::variant 访问者选择了错误的重载

c++ - 子集总和(硬币找零)