首页 文章

矢量的助推变体

提问于
浏览
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>'请求

EDIT:

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)实例化[与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>,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:instantiated来自'static typename Visitor :: result_type boost :: variant :: internal_apply_visitor_impl(int,int,Visitor&,VoidPtrCV)[with Visitor = boost :: detail :: varian t :: assign_storage,VoidPtrCV = void *,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 :: 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&)[与Visitor = boost :: detail :: variant :: assign_storage,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 :: 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&)实例化[含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 :: 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]'/softs/boost/1.53.0/64/gcc/4.5.1/include/boost/variant/variant.hpp:2175:9:从'boost :: variant&boost :: variant ::实例化运算符=(const的boost :: variant&)[与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 :: 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 >>,typename 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&)[与_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:错误:在此上下文中

提前致谢!!!!!

1 回答

  • 2

    你'll need to change the type of '零' - 它无法从 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或只比较字符串的第一个字符 .

相关问题