By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

how return a iterator?

P: n/a
VolumeType::ISetType::iterator findFirstIteratorMarked(const
VolumeType::ISetType::iterator & it,const
VolumeType::ISetType::iterator & e_it)
{
VolumeType::ISetType::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.....

Mar 17 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Mar 17, 9:04 pm, "antani" <antani8...@yahoo.itwrote:
VolumeType::ISetType::iterator findFirstIteratorMarked(const
VolumeType::ISetType::iterator & it,const
VolumeType::ISetType::iterator & e_it)
{
VolumeType::ISetType::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(noreturn)'.
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::ISetType::iterator' points to objects of
type 'vtype'*/

typedef std::vector<vtypemy_vec;

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

bool Is_not_marked(const vtype& x){/* my_vec::value_type==vtype */
return !x.mark;
};

{//somewhwere in your code:
const VolumeType::ISetType::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_marked) ;/*find first
none-marked element*/
};
Mar 17 '07 #2

P: n/a
terminator wrote:
On Mar 17, 9:04 pm, "antani" <antani8...@yahoo.itwrote:
>VolumeType::ISetType::iterator findFirstIteratorMarked(const
VolumeType::ISetType::iterator & it,const
VolumeType::ISetType::iterator & e_it)
{
VolumeType::ISetType::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(noreturn)'.
If you declare a function as '__declspec(noreturn)', 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.blogspot.com/]______\/
Mar 18 '07 #3

P: n/a
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::ISetType::iterator findFirstIteratorMarked(const
VolumeType::ISetType::iterator it,const
VolumeType::ISetType::iterator e_it)
{
VolumeType::ISetType::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
'findFirstIteratorMarked'
error C2061: syntax error : identifier 'iterator'

Mar 18 '07 #4

P: n/a
On 18 Mar, 20:09, "antani" <antani8...@yahoo.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::ISetType::iterator findFirstIteratorMarked(const
VolumeType::ISetType::iterator it,const
VolumeType::ISetType::iterator e_it)
// e.g...
typename VolumeType::ISetType::iterator findFirstIteratorMarked(const
typename VolumeType::ISetType::iterator it,const
typename VolumeType::ISetType::iterator e_it)
{
//##########
// typename ....
VolumeType::ISetType::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
'findFirstIteratorMarked'
error C2061: syntax error : identifier 'iterator'
Yep.. confusing error messages ;-)

regards
Andy Little

Mar 19 '07 #5

P: n/a
On 19 Mar, 06:07, "kwikius" <a...@servocomm.freeserve.co.ukwrote:
// Do need typename here ..
VolumeType::ISetType::iterator findFirstIteratorMarked(const
VolumeType::ISetType::iterator it,const
VolumeType::ISetType::iterator e_it)

// e.g...
typename VolumeType::ISetType::iterator findFirstIteratorMarked(const
typename VolumeType::ISetType::iterator it,const
typename VolumeType::ISetType::iterator e_it)
//OTOH Just use a typedef (note again 'typename' use) :

typedef typename VolumeType::ISetType::iterator iterator;
// now can use typedef...
iterator findFirstIteratorMarked(const
iterator it,const
iterator e_it)
>
{

//##########
// etc...
iterator _it=it;
regrads
Andy Little
Mar 19 '07 #6

P: n/a
"antani" <an********@yahoo.itwrote in message
news:11**********************@n76g2000hsh.googlegr oups.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::ISetType::iterator findFirstIteratorMarked(const
VolumeType::ISetType::iterator it,const
VolumeType::ISetType::iterator e_it)
{
VolumeType::ISetType::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
'findFirstIteratorMarked'
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::ISetType::iterator findFirstIteratorMarked(const

We see where findFirstIteraotrMarked is, look before it, so the compiler
can't figure out what
VolumeType::ISetType::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.
Mar 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.