how return a iterator?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • antani

    #1

    how return a iterator?

    VolumeType::ISe tType::iterator findFirstIterat orMarked(const
    VolumeType::ISe tType::iterator & it,const
    VolumeType::ISe tType::iterator & e_it)
    {
    VolumeType::ISe tType::iterator _it=it;
    while (_it!=e_it)
    {
    if ((*_it).mark) return _it;
    else _it++;
    }
    }


    Hi,

    I would like do a function that give a iterator to vector stl return
    the first element after of iterator where
    the field mark is true.How can I do it?
    e_it is v.end() where v is a stl vector.
    I get compiler error.....

  • terminator

    #2
    Re: how return a iterator?

    On Mar 17, 9:04 pm, "antani" <antani8...@yah oo.itwrote:
    VolumeType::ISe tType::iterator findFirstIterat orMarked(const
    VolumeType::ISe tType::iterator & it,const
    VolumeType::ISe tType::iterator & e_it)
    {
    VolumeType::ISe tType::iterator _it=it;
    while (_it!=e_it)
    {
    if ((*_it).mark) return _it;
    else _it++;
    }
    >
    }
    >
    I get compiler error.....
    You have not declared any return statement outside the 'while' loop
    which might be the reason for the error you get.

    either add a return statement outside the loop or if you are using
    microsoft vc,declare your function as '__declspec(nor eturn)'.
    Hi,
    >
    I would like do a function that give a iterator to vector stl return
    the first element after of iterator where
    the field mark is true.How can I do it?
    e_it is v.end() where v is a stl vector.
    #include <algorithm>

    and use 'std::find_if' like this:

    /*assuming that 'VolumeType::IS etType::iterato r' points to objects of
    type 'vtype'*/

    typedef std::vector<vty pemy_vec;

    bool Ismarked(const my_vec::value_t ype& x){/*
    my_vec::value_t ype==vtype */
    return x.mark;
    };

    bool Is_not_marked(c onst vtype& x){/* my_vec::value_t ype==vtype */
    return !x.mark;
    };

    {//somewhwere in your code:
    const VolumeType::ISe tType::iterator iter;
    iter=std::find_ if(v.begin(),v. end(),Ismarked) ;/*find first marked
    element*/

    iter=std::find_ if(v.begin(),v. end(),Is_not_ma rked);/*find first
    none-marked element*/
    };


    Comment

    • Adrian Hawryluk

      #3
      Re: how return a iterator?

      terminator wrote:
      On Mar 17, 9:04 pm, "antani" <antani8...@yah oo.itwrote:
      >VolumeType::IS etType::iterato r findFirstIterat orMarked(const
      >VolumeType::IS etType::iterato r & it,const
      >VolumeType::IS etType::iterato r & e_it)
      >{
      > VolumeType::ISe tType::iterator _it=it;
      > while (_it!=e_it)
      > {
      > if ((*_it).mark) return _it;
      > else _it++;
      > }
      >>
      >}
      >>
      >I get compiler error.....
      You have not declared any return statement outside the 'while' loop
      which might be the reason for the error you get.
      >
      either add a return statement outside the loop or if you are using
      microsoft vc,declare your function as '__declspec(nor eturn)'.
      If you declare a function as '__declspec(nor eturn)', wouldn't that mean
      that the returned value will either be a valid iterator or an invalid
      one? If so, then you wouldn't be able to tell them apart.

      Return the e_it instead on failure, or use the algorithms that
      terminator used.


      Adrian
      --
      _______________ _______________ _______________ _______________ _________
      \/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
      \ My newsgroup writings are licensed under the Creative Commons /
      \ Attribution-Noncommercial-Share Alike 3.0 License /
      \_____[http://creativecommons.org/licenses/...sa/3.0/]_____/
      \/______[blog:__http://adrians-musings.blogspo t.com/]______\/

      Comment

      • antani

        #4
        Re: how return a iterator?

        You have not declared any return statement outside the 'while' loop
        which might be the reason for the error you get.

        template <class MeshType, class VolumeType>
        class TrivialWalker
        {



        VolumeType::ISe tType::iterator findFirstIterat orMarked(const
        VolumeType::ISe tType::iterator it,const
        VolumeType::ISe tType::iterator e_it)
        {
        VolumeType::ISe tType::iterator _it=it;
        while (_it!=e_it)
        {
        if ((*_it).mark) return _it;
        else _it++;
        }
        return e_it;
        }

        ......
        }


        I include this class.....

        class MIVolume
        {
        public:

        typedef typename InterceptType I_Type;
        typedef typename InterceptSet<I_ TypeISetType;

        .....
        }


        I get this errors:

        error C2146: syntax error : missing ';' before identifier
        'findFirstItera torMarked'
        error C2061: syntax error : identifier 'iterator'

        Comment

        • kwikius

          #5
          Re: how return a iterator?

          On 18 Mar, 20:09, "antani" <antani8...@yah oo.itwrote:

          AFAICS..
          You need to work out how typename keyword is needed in your template
          code and not in non template code....
          >
          template <class MeshType, class VolumeType>
          class TrivialWalker
          {
          // Do need typename here ..
          VolumeType::ISe tType::iterator findFirstIterat orMarked(const
          VolumeType::ISe tType::iterator it,const
          VolumeType::ISe tType::iterator e_it)
          // e.g...
          typename VolumeType::ISe tType::iterator findFirstIterat orMarked(const
          typename VolumeType::ISe tType::iterator it,const
          typename VolumeType::ISe tType::iterator e_it)
          {
          //##########
          // typename ....
          VolumeType::ISe tType::iterator _it=it;
          //#########

          <...>
          I include this class.....
          >
          class MIVolume
          {
          public:
          //############### ############### ##
          // dont need typename here AFAICS...
          typedef typename InterceptType I_Type;
          typedef typename InterceptSet<I_ TypeISetType;
          //############### #########
          I get this errors:
          >
          error C2146: syntax error : missing ';' before identifier
          'findFirstItera torMarked'
          error C2061: syntax error : identifier 'iterator'
          Yep.. confusing error messages ;-)

          regards
          Andy Little

          Comment

          • kwikius

            #6
            Re: how return a iterator?

            On 19 Mar, 06:07, "kwikius" <a...@servocomm .freeserve.co.u kwrote:
            // Do need typename here ..
            >
            VolumeType::ISe tType::iterator findFirstIterat orMarked(const
            VolumeType::ISe tType::iterator it,const
            VolumeType::ISe tType::iterator e_it)
            >
            // e.g...
            typename VolumeType::ISe tType::iterator findFirstIterat orMarked(const
            typename VolumeType::ISe tType::iterator it,const
            typename VolumeType::ISe tType::iterator e_it)
            //OTOH Just use a typedef (note again 'typename' use) :

            typedef typename VolumeType::ISe tType::iterator iterator;
            // now can use typedef...
            iterator findFirstIterat orMarked(const
            iterator it,const
            iterator e_it)
            >
            {
            >
            //##########
            // etc...
            iterator _it=it;


            regrads
            Andy Little


            Comment

            • Jim Langston

              #7
              Re: how return a iterator?

              "antani" <antani8791@yah oo.itwrote in message
              news:1174248571 .702692.263160@ n76g2000hsh.goo glegroups.com.. .
              >
              >You have not declared any return statement outside the 'while' loop
              >which might be the reason for the error you get.
              >
              >
              template <class MeshType, class VolumeType>
              class TrivialWalker
              {
              >
              VolumeType::ISe tType::iterator findFirstIterat orMarked(const
              VolumeType::ISe tType::iterator it,const
              VolumeType::ISe tType::iterator e_it)
              {
              VolumeType::ISe tType::iterator _it=it;
              while (_it!=e_it)
              {
              if ((*_it).mark) return _it;
              else _it++;
              }
              return e_it;
              }
              >
              .....
              }
              >
              >
              I include this class.....
              >
              class MIVolume
              {
              public:
              >
              typedef typename InterceptType I_Type;
              typedef typename InterceptSet<I_ TypeISetType;
              >
              ....
              }
              >
              I get this errors:
              >
              error C2146: syntax error : missing ';' before identifier
              'findFirstItera torMarked'
              error C2061: syntax error : identifier 'iterator'
              This error, "missing ';' before identifier ..." means, if you actually
              didn't miss an ;, that the compiler didn't understand the token BEFORE the
              identifier. So we look at your code.

              VolumeType::ISe tType::iterator findFirstIterat orMarked(const

              We see where findFirstIterao trMarked is, look before it, so the compiler
              can't figure out what
              VolumeType::ISe tType::iterator is. Maybe it doesn't know what VolumeType
              is. Maybe it doesn't know what ISetType is.

              We see that VolumeType is a template parameter, so that's probably not it.
              We don't see ISetType passed. So, where is ISetType defined? It most
              likely isn't find it for that class, maybe you didn't define it correctly,
              or before it was needed.


              Comment

              Working...