2

我尝试使用 boost::variant

std::vector<boost::variant<mesh::Entity&, mesh::Vertex, mesh::Edge> > 
    xField::getZeros(const mesh::Region& s, char *EntityS) const
{
//Vertex  
  if( EntityS == "V" )
  {
    std::vector<mesh::Vertex> zeros;
    for(Region::vertexIterator itv = s.beginVertex(); 
        itv != s.endVertex(); ++itv )
    {
      if( abs(getVal(*itv)) < 1.e-12 )      
      zeros.push_back(*itv);
    }
      return zeros;    
  }
 else if ..

但我收到错误:错误:从“std::vector”转换为非标量类型“std::vector >”请求

编辑:

zeroVector xField::getZeros(const mesh::Region& s, char *EntityS) const
{
//Vertex  
  if( EntityS == "V" )
  {
    zeroVector zeros;
    for(Region::vertexIterator itv = s.beginVertex(); itv != s.endVertex(); ++itv )
    {
      if( abs(getVal(*itv)) < 1.e-12 )      
      zeros.push_back( zeroVariant(*itv) );
    }
      return zeros;

我在 .h 文件中定义 typedef

typedef boost::variant<mesh::Entity&, mesh::Vertex, mesh::Edge> zeroVariant;
typedef std::vector<zeroVariant> zeroVector;

现在我有以下错误:

/softs/boost/1.53.0/64/gcc/4.5.1/include/boost/detail/reference_content.hpp: 在成员函数'void boost::detail::variant::assign_storage::internal_visit(T&, int) const [with T = boost::detail::reference_content]': /softs/boost/1.53.0/64/gcc/4.5.1/include/boost/variant/detail/visitation_impl.hpp:130:9: 实例化自'typename Visitor::result_type boost::detail::variant::visitation_impl_invoke_impl(int, Visitor&, VoidPtrCV, T*, mpl_::true_) [with Visitor = boost::detail::variant::assign_storage, VoidPtrCV = void* , T = boost::detail::reference_content, typename Visitor::result_type = void, mpl_::true_ = mpl_::bool_]' /softs/boost/1.53.0/64/gcc/4.5.1/include/boost /variant/detail/visitation_impl.hpp:173:9: 从 'typename Visitor::result_type boost::detail::variant::visitation_impl_invoke(int, Visitor&, VoidPtrCV, T*,NoBackupFlag, int) [with Visitor = boost::detail::variant::assign_storage, VoidPtrCV = void*, T = boost::detail::reference_content, NoBackupFlag = boost::variant::has_fallback_type_, typename Visitor::result_type = void]' /softs/boost/1.53.0/64/gcc/4.5.1/include/boost/variant/detail/visitation_impl.hpp:260:1: 从 '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::detail::reference_content, boost::mpl::l_item, mesh::Vertex, boost::mpl::l_item, mesh::Edge, boost::mpl::l_end> > > >, boost:: mpl::l_iter >, 访问者 = boost::detail::variant::assign_storage, VoidPtrCV = void*, NoBackupFlag = boost::variant::has_fallback_type_,类型名访问者::result_type = void, mpl_::false_ = mpl_::bool_]' /softs/boost/1.53.0/64/gcc/4.5.1/include/boost/variant/variant.hpp:2326:13:从 '静态类型名 Visitor::result_type boost::variant::internal_apply_visitor_impl(int, int, Visitor&, VoidPtrCV) [with Visitor = boost::detail::variant::assign_storage, VoidPtrCV = void*, T0_ = mesh::实体&, T1 = 网格::顶点, T2 = 网格::边, T3 = boost::detail::variant::void_, T4 = boost::detail::variant::void_, T5 = boost::detail::变量::void_, T6 = boost::detail::variant::void_, T7 = boost::detail::variant::void_, T8 = boost::detail::variant::void_, T9 = boost::detail ::variant::void_, T10 = boost::detail::variant::void_, T11 = boost::detail::variant::void_, T12 = boost::detail::variant::void_, T13 = boost: :detail::variant::void_, T14 = boost::detail::variant::void_,T15 = boost::detail::variant::void_, T16 = boost::detail::variant::void_, T17 = boost::detail::variant::void_, T18 = boost::detail::variant:: void_, T19 = boost::detail::variant::void_, typename Visitor::result_type = void]' /softs/boost/1.53.0/64/gcc/4.5.1/include/boost/variant/variant.hpp :2337:13: 从 'typename Visitor::result_type boost::variant::internal_apply_visitor(Visitor&) [with Visitor = boost::detail::variant::assign_storage, T0_ = mesh::Entity&, T1 = mesh::顶点,T2 = 网格::边,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::void_, T9 = boost::detail::variant::void_, T10 = boost::detail::variant::void_, T11 = boost::detail::variant::void_, T12 = boost::detail::variant::void_, T13 = boost::detail::variant::void_, T14 = boost::detail::variant::void_, T15 = boost::detail::variant::void_, T16 = boost::detail::variant::void_, T17 = boost::detail::variant::void_, T18 = boost::detail::variant::void_, T19 = boost::detail::variant::void_, typename Visitor::result_type = void]' /softs/boost/1.53.0/64/gcc/4.5.1/include/boost/variant/variant.hpp:2085:13: 从 'void boost::variant:: 实例化variant_assign(const boost::variant&) [with T0_ = mesh::Entity&, T1 = mesh::Vertex, T2 = mesh::Edge, 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::void_, T9 = boost::detail::variant::void_, T10 = boost::detail::variant::void_, T11 = boost::detail::变量::void_, T12 = boost::detail::variant::void_, T13 = boost::detail::variant::void_, T14 = boost::detail::variant::void_, T15 = boost::detail ::variant::void_, T16 = boost::detail::variant::void_, T17 = boost::detail::variant::void_, T18 = boost::detail::variant::void_, T19 = boost: :detail::variant::void_, boost::variant = boost::variant]' /softs/boost/1.53.0/64/gcc/4.5.1/include/boost/variant/variant.hpp:2175:9 : 从 'boost::variant& boost::variant::operator=(const boost::variant&) [with T0_ = mesh::Entity&, T1 = mesh::Vertex, T2 = mesh::Edge, 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::void_, T9 = boost::detail::variant::void_, T10 = boost::detail::变量::void_, T11 = boost::detail::variant::void_, T12 = boost::detail::variant::void_, T13 = boost::detail::variant::void_, T14 = boost::detail ::variant::void_, T15 = boost::detail::variant::void_, T16 = boost::detail::variant::void_, T17 = boost::detail::variant::void_, T18 = boost: :detail::variant::void_, T19 = boost::detail::variant::void_, boost::variant = boost::variant]' /usr/include/c++/4.5/bits/vector.tcc:312: 4:从 'void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, const _Tp&) 实例化 [with _Tp = boost::variant, _Alloc = std::allocator >, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator*, std::vector > >, 类型名 std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = boost::variant* ]' /usr/include/c++/4.5/bits/stl_vector.h:749:4:从 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = boost::variant, _Alloc = std::allocator >, value_type = boost::variant]' /home/mycode. cc:128:42:从这里/softs/boost/1.53.0/64/gcc/4.5.1/include/boost/detail/reference_content.hpp:62:24 实例化:错误:'boost::detail::reference_content& boost::detail::reference_content::operator=(const boost::detail::reference_content&) [with RefT = mesh::Entity&, boost::detail::reference_content = boost::detail::reference_content]' 是私有的 / softs/boost/1.53.0/64/gcc/4.5.1/include/boost/variant/variant.hpp:526:9:错误:在此上下文中从这里/softs/boost/1.53.0/64/gcc/4.5.1/include/boost/detail/reference_content.hpp:62:24 实例化:错误:'boost::detail::reference_content& boost::detail:: reference_content::operator=(const boost::detail::reference_content&) [with RefT = mesh::Entity&, boost::detail::reference_content = boost::detail::reference_content]' 是私有的 /softs/boost/1.53。 0/64/gcc/4.5.1/include/boost/variant/variant.hpp:526:9:错误:在此上下文中从这里/softs/boost/1.53.0/64/gcc/4.5.1/include/boost/detail/reference_content.hpp:62:24 实例化:错误:'boost::detail::reference_content& boost::detail:: reference_content::operator=(const boost::detail::reference_content&) [with RefT = mesh::Entity&, boost::detail::reference_content = boost::detail::reference_content]' 是私有的 /softs/boost/1.53。 0/64/gcc/4.5.1/include/boost/variant/variant.hpp:526:9:错误:在此上下文中1/include/boost/variant/variant.hpp:526:9:错误:在此上下文中1/include/boost/variant/variant.hpp:526:9:错误:在此上下文中

提前致谢!!!!!

4

1 回答 1

3

您需要更改“零”的类型 - 它无法转换std::vector<mesh::Vertex>为您的变体类型的向量。使用一些 typedef 也可以使代码不那么冗长:

typedef boost::variant<mesh::Entity&, mesh::Vertex, mesh::Edge> EvVariant;
typedef std::vector<EvVariant> EvVariantVector;

EvVariantVector xField::getZeros(const mesh::Region& s, char *EntityS) const
{
//Vertex  
  if( EntityS == "V" ) // This line is probably a bug!
  {
    EvVariantVector zeros; // Use the return type here!
    for(Region::vertexIterator itv = s.beginVertex(); itv != s.endVertex(); ++itv )
    {
      if( abs(getVal(*itv)) < 1.e-12 )      
      zeros.push_back(EvVariant(*itv));
    }
      return zeros;    
  }
 else if ..

此外,该行if (EntityS == "V")不正确,可能会失败。使用 strcmp 或只比较字符串的第一个字符。

于 2013-07-15T10:29:18.320 回答