矢量的助推变体

时间:2013-07-15 10:24:44

标签: c++ boost vector variant boost-variant

我尝试使用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&gt;'请求

修改

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&amp; ;,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&amp;,VoidPtrCV,T *,mpl _ :: true_)[与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&amp;,VoidPtrCV,T *,NoBackupFlag,int)[与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&amp;,VoidPtrCV,mpl _ :: false_,NoBackupFlag,which *,step0 *)[with which = mpl _ :: int_&lt; 0&gt ;, 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&gt; &GT; &GT; &gt;,boost :: mpl :: l_iter&gt ;, Visitor = boost :: detail :: variant :: assign_storage,VoidPtrCV = void *,NoBackupFlag = boost :: variant :: has_fallback_type_,typename Visitor :: result_type = void,mpl_ :: false_ = mpl _ :: bool_]' /softs/boost/1.53.0/64/gcc/4.5.1/include/boost/variant/variant.hpp:2326:13:从'static typename实例化访问者:: result_type boost :: variant :: internal_apply_visitor_impl(int, int,Visitor&amp;,VoidPtrCV)[与Visitor = boost :: detail :: variant :: assign_storage,VoidPtrCV = void *,T0_ = mesh :: Entity&amp;,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 :: 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_ty pe = 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&amp;)实例化[与Visitor = boost :: detail :: variant :: assign_storage,T0_ = mesh :: Entity&amp;,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 :: 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&amp;)实例化[使用T0_ = mesh :: Entity&amp;,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 :: 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 =提高::变种]” /softs/boost/1.53.0/64/gcc/4.5.1/include/boost/variant/variant.hpp:2175:9:从'boost :: variant&amp; amp;实例化boost :: variant :: operator =(const boost :: variant&amp;)[with T0_ = mesh :: Entity&amp;,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 :: 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&lt; _Tp,_Alloc&gt; :: _ M_insert_aux实例化(std :: vector&lt; _Tp,_Alloc&gt; :: iterator,const _Tp&amp;)[with _Tp = boost :: variant,_Alloc = std :: allocator&gt;,std :: vector&lt; _Tp,_Alloc&gt; :: iterator = __gnu_cxx :: __ normal_iterator *,std :: vector&gt; &gt;,typename std :: _ Vector_base&lt; _Tp,_Alloc&gt; :: _ Tp_alloc_type :: pointer = boost :: variant *]' /usr/include/c++/4.5/bits/stl_vector.h:749:4:从'void std :: vector&lt; _Tp,_Alloc&gt; :: push_back(const value_type&amp;)实例化[with _Tp = boost :: variant,_Alloc = std :: allocator&gt;,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&amp; boost :: detail :: reference_content :: operator =(const boost :: detail :: reference_content&amp;)[with RefT = mesh :: Entity&amp;,boost :: detail :: reference_content = boost :: detail :: reference_content]'is私人的 /softs/boost/1.53.0/64/gcc/4.5.1/include/boost/variant/variant.hpp:526:9:错误:在此上下文中

提前致谢!!!!!

1 个答案:

答案 0 :(得分: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或只比较字符串的第一个字符。