编译文件的变体问题

时间:2011-05-17 17:29:54

标签: c++ boost boost-variant

我尝试从此页面编译以下代码: http://www.pdc.kth.se/training/Talks/C++/boost/libs/variant/doc/sample.html

在“二叉树实现”下,我遇到了大量错误,无法解决问题。我正在使用带代码块的gcc 3.4.5。

#include <iostream>

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


using boost::variant;
using boost::incomplete;
using std::cout;
using std::endl;


struct non_leaf_node; // Forward declaration

// Define a variant with these two types:
//   1) int
//   2) The (incomplete) non_leaf_node struct
typedef variant<int, incomplete<non_leaf_node> > tree_node;

struct non_leaf_node
{
   non_leaf_node(const tree_node& l, int num, const tree_node& r)
      :  left_(l), right_(r), num_(num) { }
   non_leaf_node(const non_leaf_node& other)
      :  left_(other.left_), right_(other.right_), num_(other.num_)  { }

   int num_;
   tree_node left_;
   tree_node right_;
};


struct tree_printer : boost::static_visitor<void>
{
   void operator()(int n) const
   {
      cout << n << ' ';
   }

   void operator()(const non_leaf_node& node) const
   {
      boost::apply_visitor(*this, node.left_);
      cout << node.num_ << ' ';
      boost::apply_visitor(*this, node.right_);
   }
};


int main(int, char* [] )
{
   //Build a binary search tree:
   non_leaf_node a(3,4, 6);
   non_leaf_node b(19, 20, 23);
   non_leaf_node c(a,10, b);

   tree_node root(c);

   //Perform an in-order walk
   boost::apply_visitor(tree_printer(), root);

   return 0;
}

编译期间的追溯:

> C:\boostvariantBtree\boostvariantBtree.cpp||In
> constructor
> `non_leaf_node::non_leaf_node(const
> tree_node&, int, const tree_node&)':|
> C:\boostvariantBtree\boostvariantBtree.cpp|24|warning:
> `non_leaf_node::right_' will be
> initialized after|
> C:\boostvariantBtree\boostvariantBtree.cpp|22|warning:
> `int non_leaf_node::num_'|
> C:\boostvariantBtree\boostvariantBtree.cpp|19|warning:
> when initialized here|
> C:\boostvariantBtree\boostvariantBtree.cpp|24|warning:
> `non_leaf_node::right_' will be
> initialized after|
> C:\boostvariantBtree\boostvariantBtree.cpp|22|warning:
> `int non_leaf_node::num_'|
> C:\boostvariantBtree\boostvariantBtree.cpp|20|warning:
> when initialized here| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp||In
> member function `typename
> Visitor::result_type
> boost::detail::variant::invoke_visitor<Visitor>::internal_visit(T&,
> int) [with T = const
> boost::incomplete<non_leaf_node>,
> Visitor = const tree_printer]':|
> C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\visitation_impl.hpp|128|instantiated
> from `typename Visitor::result_type
> boost::detail::variant::visitation_impl_invoke_impl(int,
> Visitor&, VoidPtrCV, T*, mpl_::true_)
> [with Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, VoidPtrCV = const
> void*, T =
> boost::incomplete<non_leaf_node>]'|
> C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\visitation_impl.hpp|170|instantiated
> from `typename Visitor::result_type
> boost::detail::variant::visitation_impl_invoke(int,
> Visitor&, VoidPtrCV, T*, NoBackupFlag,
> int) [with Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, VoidPtrCV = const
> void*, T =
> boost::incomplete<non_leaf_node>,
> NoBackupFlag = boost::variant<int,
> boost::incomplete<non_leaf_node>,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::detail::variant::void_, boo|
> C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\visitation_impl.hpp|256|instantiated
> from `typename Visitor::result_type
> boost::detail::variant::visitation_impl(int,
> int, Visitor&, VoidPtrCV,
> mpl_::false_, NoBackupFlag, Which*,
> step0*) [with Which = mpl_::int_<0>,
> step0 =
> boost::detail::variant::visitation_impl_step<boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>,
> int,
> boost::mpl::l_item<mpl_::long_<1l>,
> boost::incomplete<non_leaf_node>,
> boost::mpl::l_end> > >,
> boost::mpl::l_iter<boost::mpl::l_end>
> >, Visitor = boost::detail::variant::invoke_visitor<const
> tree_printer>, | C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|1771|instantiated
> from `static typename
> Visitor::result_type
> boost::variant<T0, T1, T2, T3, T4, T5,
> T6, T7, T8, T9, T10, T11, T12, T13,
> T14, T15, T16, T17, T18,
> T19>::internal_apply_visitor_impl(int,
> int, Visitor&, VoidPtrCV) [with
> Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, VoidPtrCV = const
> void*, T0_ = int, T1 =
> boost::incomplete<non_leaf_node>, T2 =
> boost::detail::variant::void_, T3 =
> boost::detail::variant::void_, T4 =
> boost::detail::variant::void_, T5 =
> boost::detail::variant::vo| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|1796|instantiated
> from `typename Visitor::result_type
> boost::variant<T0, T1, T2, T3, T4, T5,
> T6, T7, T8, T9, T10, T11, T12, T13,
> T14, T15, T16, T17, T18,
> T19>::internal_apply_visitor(Visitor&)
> const [with Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, T0_ = int, T1 =
> boost::incomplete<non_leaf_node>, T2 =
> boost::detail::variant::void_, T3 =
> boost::detail::variant::void_, T4 =
> boost::detail::variant::void_, T5 =
> boost::detail::variant::void_, T6 =
> boost::detail::variant::void_, T7 =
> boost:| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|1820|instantiated
> from `typename Visitor::result_type
> boost::variant<T0, T1, T2, T3, T4, T5,
> T6, T7, T8, T9, T10, T11, T12, T13,
> T14, T15, T16, T17, T18,
> T19>::apply_visitor(Visitor&) const
> [with Visitor = const tree_printer,
> T0_ = int, T1 =
> boost::incomplete<non_leaf_node>, T2 =
> boost::detail::variant::void_, T3 =
> boost::detail::variant::void_, T4 =
> boost::detail::variant::void_, T5 =
> boost::detail::variant::void_, T6 =
> boost::detail::variant::void_, T7 =
> boost::detail::variant::void_, T8 =
> boost::detail::vari| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\apply_visitor_unary.hpp|76|instantiated
> from `typename Visitor::result_type
> boost::apply_visitor(const Visitor&,
> Visitable&) [with Visitor =
> tree_printer, Visitable = const
> tree_node]'|
> C:\boostvariantBtree\boostvariantBtree.cpp|34|instantiated
> from here| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|832|error:
> no match for call to `(const
> tree_printer) (const
> boost::incomplete<non_leaf_node>&)'|
> C:\boostvariantBtree\boostvariantBtree.cpp|29|note:
> candidates are: void
> tree_printer::operator()(int) const|
> C:\boostvariantBtree\boostvariantBtree.cpp|33|note:
> void tree_printer::operator()(const
> non_leaf_node&) const| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|832|error:
> return-statement with a value, in
> function returning 'void'| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp||In
> member function `typename
> Visitor::result_type
> boost::detail::variant::invoke_visitor<Visitor>::internal_visit(T&,
> int) [with T =
> boost::incomplete<non_leaf_node>,
> Visitor = const tree_printer]':|
> C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\visitation_impl.hpp|128|instantiated
> from `typename Visitor::result_type
> boost::detail::variant::visitation_impl_invoke_impl(int,
> Visitor&, VoidPtrCV, T*, mpl_::true_)
> [with Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, VoidPtrCV = void*, T =
> boost::incomplete<non_leaf_node>]'|
> C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\visitation_impl.hpp|170|instantiated
> from `typename Visitor::result_type
> boost::detail::variant::visitation_impl_invoke(int,
> Visitor&, VoidPtrCV, T*, NoBackupFlag,
> int) [with Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, VoidPtrCV = void*, T =
> boost::incomplete<non_leaf_node>,
> NoBackupFlag = boost::variant<int,
> boost::incomplete<non_leaf_node>,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::detail::variant::void_,
> boost::de| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\visitation_impl.hpp|256|instantiated
> from `typename Visitor::result_type
> boost::detail::variant::visitation_impl(int,
> int, Visitor&, VoidPtrCV,
> mpl_::false_, NoBackupFlag, Which*,
> step0*) [with Which = mpl_::int_<0>,
> step0 =
> boost::detail::variant::visitation_impl_step<boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>,
> int,
> boost::mpl::l_item<mpl_::long_<1l>,
> boost::incomplete<non_leaf_node>,
> boost::mpl::l_end> > >,
> boost::mpl::l_iter<boost::mpl::l_end>
> >, Visitor = boost::detail::variant::invoke_visitor<const
> tree_printer>, | C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|1771|instantiated
> from `static typename
> Visitor::result_type
> boost::variant<T0, T1, T2, T3, T4, T5,
> T6, T7, T8, T9, T10, T11, T12, T13,
> T14, T15, T16, T17, T18,
> T19>::internal_apply_visitor_impl(int,
> int, Visitor&, VoidPtrCV) [with
> Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, VoidPtrCV = void*, T0_
> = int, T1 = boost::incomplete<non_leaf_node>, T2 =
> boost::detail::variant::void_, T3 =
> boost::detail::variant::void_, T4 =
> boost::detail::variant::void_, T5 =
> boost::detail::variant::void_, T|
> C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|1785|instantiated
> from `typename Visitor::result_type
> boost::variant<T0, T1, T2, T3, T4, T5,
> T6, T7, T8, T9, T10, T11, T12, T13,
> T14, T15, T16, T17, T18,
> T19>::internal_apply_visitor(Visitor&)
> [with Visitor =
> boost::detail::variant::invoke_visitor<const
> tree_printer>, T0_ = int, T1 =
> boost::incomplete<non_leaf_node>, T2 =
> boost::detail::variant::void_, T3 =
> boost::detail::variant::void_, T4 =
> boost::detail::variant::void_, T5 =
> boost::detail::variant::void_, T6 =
> boost::detail::variant::void_, T7 =
> boost::detai| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|1810|instantiated
> from `typename Visitor::result_type
> boost::variant<T0, T1, T2, T3, T4, T5,
> T6, T7, T8, T9, T10, T11, T12, T13,
> T14, T15, T16, T17, T18,
> T19>::apply_visitor(Visitor&) [with
> Visitor = const tree_printer, T0_ =
> int, T1 =
> boost::incomplete<non_leaf_node>, T2 =
> boost::detail::variant::void_, T3 =
> boost::detail::variant::void_, T4 =
> boost::detail::variant::void_, T5 =
> boost::detail::variant::void_, T6 =
> boost::detail::variant::void_, T7 =
> boost::detail::variant::void_, T8 =
> boost::detail::variant::v| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\detail\apply_visitor_unary.hpp|76|instantiated
> from `typename Visitor::result_type
> boost::apply_visitor(const Visitor&,
> Visitable&) [with Visitor =
> tree_printer, Visitable = tree_node]'|
> C:\boostvariantBtree\boostvariantBtree.cpp|49|instantiated
> from here| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|832|error:
> no match for call to `(const
> tree_printer)
> (boost::incomplete<non_leaf_node>&)'|
> C:\boostvariantBtree\boostvariantBtree.cpp|29|note:
> candidates are: void
> tree_printer::operator()(int) const|
> C:\boostvariantBtree\boostvariantBtree.cpp|33|note:
> void tree_printer::operator()(const
> non_leaf_node&) const| C:\Program
> Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\boost\variant\variant.hpp|832|error:
> return-statement with a value, in
> function returning 'void'| ||=== Build
> finished: 4 errors, 6 warnings ===|

1 个答案:

答案 0 :(得分:2)

据我所知,目前boost似乎没有incomplete.hpp。 如果您的boost是最近的, 删除该行 #include "boost/incomplete.hpp"using boost::incomplete;
并替换行

typedef variant<int, incomplete<non_leaf_node> > tree_node;

typedef variant<int, boost::recursive_wrapper<non_leaf_node> > tree_node;

可能会编译代码。 这是对ideone的测试 如果你需要递归类型, 这个文件 Recursive variant types 可能有所帮助。