提升变量apply_visitor编译错误

 mobiledu2502909217 发布于 2023-02-10 09:40

这个简单的示例代码为boost :: variant和boost :: apply_visitor:

#include 

struct ExprFalse;
struct ExprTrue;
struct ExprMaybe;

typedef boost::variant<
    ExprFalse,
    ExprTrue,
    ExprMaybe
    > Expression;

struct ExprFalse { };
struct ExprTrue { };
struct ExprMaybe { };

struct Printer : public boost::static_visitor<>
{
public:
    Printer(std::ostream& os) : m_os(os) { }
    void operator()(ExprFalse const& expr) const { m_os << "False"; }
    void operator()(ExprTrue const& expr) const { m_os << "True"; }
    void operator()(ExprMaybe const& expr) const { m_os << "Maybe"; }

private:
    std::ostream& m_os;
};

int main()
{
    Expression e(ExprTrue());
    boost::apply_visitor(Printer(std::cout), e);
    return 0;
}

产生以下编译错误:

g++-mp-4.8 -MMD -DBOOST_ALL_DYN_LINK -DBOOST_SPIRIT_USE_PHOENIX_V3 -Wall -std=c++11 -Os -O3 -g -I/o\
pt/local/include -I./  -c tools/t6.cpp -o tools/build/x86_64/objs/t6.o
In file included from /opt/local/include/boost/variant/apply_visitor.hpp:16:0,
                 from /opt/local/include/boost/variant/detail/hash_variant.hpp:23,
                 from /opt/local/include/boost/variant/variant.hpp:37,
                 from /opt/local/include/boost/variant/recursive_variant.hpp:36,
                 from tools/t6.cpp:4:
/opt/local/include/boost/variant/detail/apply_visitor_unary.hpp: In instantiation of 'typename Visi\
tor::result_type boost::apply_visitor(const Visitor&, Visitable&) [with Visitor = Printer; Visitabl\
e = boost::variant(ExprTrue (*)()); typename Visitor::result_type =\
 void]':
tools/t6.cpp:35:47:   required from here
/opt/local/include/boost/variant/detail/apply_visitor_unary.hpp:76:43: error: request for member 'a\
pply_visitor' in 'visitable', which is of non-class type 'boost::variant(ExprTrue (*)())'
     return visitable.apply_visitor(visitor);
                                           ^
/opt/local/include/boost/variant/detail/apply_visitor_unary.hpp:76:43: error: return-statement with\
 a value, in function returning 'void' [-fpermissive]
make: *** [tools/build/x86_64/objs/t6.o] Error 1

在Mac OSX Mavericks上使用Boost版本1.55.0.

对于我的生活,我无法弄清楚这个问题.我试过实际上有一个返回类型(即使打印访问者不需要一个),但我最终得到了相同的错误.

任何见解将不胜感激.

1 个回答
  • 你被最令人烦恼的解析规则所击中:e实际上是一个函数.再添加一对括号:

    Expression e((ExprTrue()));
    

    2023-02-10 09:43 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有