For Programmers: Free Programming Magazines  


Home > Archive > VC STL > February 2006 > accessing static array of structs









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author accessing static array of structs
Al

2006-02-15, 7:04 pm

Have STL question:

Just tried compiling the some code with visual studio 2005. it can’t get
past this code which VS 2003 had no problem with:

I have a static array of structs which I want to operate on

Struct SOMESTRUCT
{
Int i1;
Int i2;
};


typedef std::vector<SOMESTRUCT>::iterator someStructIter ;
Const int NUM = 4 ;
SOMESTRUCT anArrayOfSomeStructs [NUM] =
{
…… the static initializers
};

someFunc()
{

for (someStructIter it = anArrayOfSomeStructs ; &*it != &
anArrayOfSomeStructs [NUM] ; ++it)
.. do something nasty to it
}

vs2005 doesn’t like the iter assignment:
someStructIter it = anArrayOfSomeStructs

thanks
Al

red floyd

2006-02-15, 7:04 pm

Al wrote:
> Have STL question:
>
> Just tried compiling the some code with visual studio 2005. it can’t get
> past this code which VS 2003 had no problem with:
>
> I have a static array of structs which I want to operate on
>
> Struct SOMESTRUCT
> {
> Int i1;
> Int i2;
> };
>
>
> typedef std::vector<SOMESTRUCT>::iterator someStructIter ;
> Const int NUM = 4 ;
> SOMESTRUCT anArrayOfSomeStructs [NUM] =
> {
> …… the static initializers
> };
>
> someFunc()
> {
> …
> for (someStructIter it = anArrayOfSomeStructs ; &*it != &
> anArrayOfSomeStructs [NUM] ; ++it)
> .. do something nasty to it
> }
>
> vs2005 doesn’t like the iter assignment:
> someStructIter it = anArrayOfSomeStructs



because an iterator into a vector is not a pointer. If you want to use
a vector use a vector. If you want to use an array, use a pointer.

e.g.

std::vector<SOMESTRUCT> v(anArrayOfSomeStructs,anArrayOfSomeStru
ct+NUM);
for (someStructIter it = v.begin(); it != v.end; ++v)
{
}

or

for (SOMESTRUCT* p = anArrayOfSomeStruct;
p != anArrayOfSomeStruct + NUM;
++p)
{
}

but you can't mix them the way you did.

Al

2006-02-15, 7:04 pm

thanks .... that fixed it.

the way i had it did work in vs2003 though. :o)


--
Al



"red floyd" wrote:

> Al wrote:
>
>
> because an iterator into a vector is not a pointer. If you want to use
> a vector use a vector. If you want to use an array, use a pointer.
>
> e.g.
>
> std::vector<SOMESTRUCT> v(anArrayOfSomeStructs,anArrayOfSomeStru
ct+NUM);
> for (someStructIter it = v.begin(); it != v.end; ++v)
> {
> }
>
> or
>
> for (SOMESTRUCT* p = anArrayOfSomeStruct;
> p != anArrayOfSomeStruct + NUM;
> ++p)
> {
> }
>
> but you can't mix them the way you did.
>
>

Tom Widmer [VC++ MVP]

2006-02-16, 8:01 am

Al wrote:
> thanks .... that fixed it.
>
> the way i had it did work in vs2003 though. :o)


Still, programming solely by trial and error, rather than reading
documentation to determine how to do something, is generally not a good
idea.

Tom
Al

2006-02-16, 7:05 pm

point taken :o)


--
Al



"Tom Widmer [VC++ MVP]" wrote:

> Al wrote:
>
> Still, programming solely by trial and error, rather than reading
> documentation to determine how to do something, is generally not a good
> idea.
>
> Tom
>

Al

2006-02-17, 7:02 pm

BTW ... is there a known problem with vs2005 locking up XP?

i'm using vs2005 on several large projects and XP is totally locking up
about once a day. i never saw this with vs2003.

thanks again

--
Al



"Tom Widmer [VC++ MVP]" wrote:

> Al wrote:
>
> Still, programming solely by trial and error, rather than reading
> documentation to determine how to do something, is generally not a good
> idea.
>
> Tom
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com