Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对boost::static\u访问者的要求_Boost_Boost Variant - Fatal编程技术网

对boost::static\u访问者的要求

对boost::static\u访问者的要求,boost,boost-variant,Boost,Boost Variant,我对boost::static_visitor for的应用程序有点困惑 变体和结构。我在下面包含了一个测试用例。对于 “s_访问者”中被注释掉的部分,我不明白为什么 出现以下错误消息或如何修复它: 应用访问者一元。hpp:72:错误:“结构s1”没有名为“应用访问者”的成员 #include "boost/variant.hpp" #include "iostream" struct s1 { int val; s1(int a) : val(a) {} }; struct s2

我对boost::static_visitor for的应用程序有点困惑 变体和结构。我在下面包含了一个测试用例。对于 “s_访问者”中被注释掉的部分,我不明白为什么 出现以下错误消息或如何修复它:

应用访问者一元。hpp:72:错误:“结构s1”没有名为“应用访问者”的成员

#include "boost/variant.hpp"
#include "iostream"

struct s1 {
  int val;

  s1(int a) : val(a) {}
};

struct s2 {
  s1  s;
  int val;

  s2(int a, int b) : s(a), val(b) {}
};

struct s_visitor : public boost::static_visitor<>
{
    void operator()(int & i) const
    {
        std::cout << "int" << std::endl;
    }

    void operator()(s1 & s) const
    {
        std::cout << "s1" << std::endl;
    }

    void operator()(s2 & s) const
    {
        std::cout << "s2" << std::endl;
        // -> following 'struct s1' has no member apply_visitor
        // boost::apply_visitor(s_visitor(), s.s);
        // -> following 'struct s1' has no member apply_visitor
        // boost::apply_visitor(*this, s.s);
        s_visitor v;
        v(s.s);
    }
};

int main(int argc, char **argv)
{
  boost::variant< int, s1, s2 > v;
  s1 a(1);
  s2 b(2, 3);

  v = a;
  boost::apply_visitor(s_visitor(), v);

  v = b;
  boost::apply_visitor(s_visitor(), v);

  return 0;
}
#包括“boost/variant.hpp”
#包括“iostream”
结构s1{
int-val;
s1(inta):val(a){}
};
结构s2{
s1s;
int-val;
s2(inta,intb):s(a),val(b){}
};
结构s_访问者:公共boost::static_访问者
{
void运算符()(int&i)常量
{

std::cout两个注释掉的行都会出现编译错误,因为您传递的是一个“s1”,其中需要boost::variant。但是,在代码中的这一点上,您知道确切的类型,因此不需要进行变量访问,您只需使用s1类型的值执行任何操作即可。

@dan10400“s_visitor v;v(s.s);”编译?我认为它会,或者我认为“(*this)(s.s)”会起作用。谢谢。这个例子是从一个更大的例子中总结出来的,在这个例子中,结构与变量混合在一起,我想我是在推动一些用于遍历数据结构的集成方法。代码“s_visitor v;v(s.s);“进行编译和工作,因为它对类型使用运算符()。这可能增加了我的困惑。但是代码“(*This)(s.s)”没有。