C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

creating a std::vector<T>::iterator ???

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
aaragon
Guest





PostPosted: Tue Sep 19, 2006 9:10 am    Post subject: creating a std::vector<T>::iterator ??? Reply with quote



I am trying to create a vector of type T and everything goes fine until
I try to iterate over it. For some reason, the compiler gives me an
error when I declare
std::vector<T>::iterator iter;
Any ideas why is tihs happening? The code is as follows:

template <class T>
struct StdVectorStorage
{
std::vector<T>* _storage;

void create(size_t p,size_t l)
{
_storage = new std::vector<T>(p);

std::vector<T>::iterator iter;

}

~StdVectorStorage()
{
}
};

g++ -c -o main.o main.cxx -pg -O2 -Wall -Wno-sign-compare
gaPolicies.h: In member function 'void
StdVectorStorage<T>::create(size_t, size_t)':
gaPolicies.h:37: error: expected `;' before 'iter'
gaPopulation.h: In member function 'void Population<Individual,
StoragePolicy>::initialize(gaParameters*) [with Individual =
Individual<Chromosome<BoostBitsetStorage>, HeapStorage>, StoragePolicy
= StdVectorStorage]':
gaSimpleGA.h:152: instantiated from 'void gaSimple<Population,
SelectionPolicy>::initialize(int, char**) [with Population =
Population<Individual<Chromosome<BoostBitsetStorage>, HeapStorage>,
StdVectorStorage>, SelectionPolicy = ProportionateSelector]'
main.cxx:16: instantiated from here
gaPopulation.h:36: warning: unused variable 'xsite_'
gaPolicies.h: In member function 'void
StdVectorStorage<T>::create(size_t, size_t) [with T =
Individual<Chromosome<BoostBitsetStorage>, HeapStorage>]':
gaPopulation.h:40: instantiated from 'void Population<Individual,
StoragePolicy>::initialize(gaParameters*) [with Individual =
Individual<Chromosome<BoostBitsetStorage>, HeapStorage>, StoragePolicy
= StdVectorStorage]'
gaSimpleGA.h:152: instantiated from 'void gaSimple<Population,
SelectionPolicy>::initialize(int, char**) [with Population =
Population<Individual<Chromosome<BoostBitsetStorage>, HeapStorage>,
StdVectorStorage>, SelectionPolicy = ProportionateSelector]'
main.cxx:16: instantiated from here
gaPolicies.h:37: error: dependent-name
'std::vector<T,std::allocator<_CharT> >::iterator' is parsed as a
non-type, but instantiation yields a type
gaPolicies.h:37: note: say 'typename
std::vector<T,std::allocator<_CharT> >::iterator' if a type is meant
make: *** [main.o] Error 1=
Back to top
Marco Wahl
Guest





PostPosted: Tue Sep 19, 2006 9:10 am    Post subject: Re: creating a std::vector<T>::iterator ??? Reply with quote



aaragon schrieb:

Quote:
I am trying to create a vector of type T and everything goes fine until
I try to iterate over it. For some reason, the compiler gives me an
error when I declare
std::vector<T>::iterator iter;
Any ideas why is tihs happening? The code is as follows:

template <class T
struct StdVectorStorage
{
std::vector<T>* _storage;

void create(size_t p,size_t l)
{
_storage = new std::vector<T>(p);


Maybe the compiler needs a suggestion here:

Quote:
std::vector<T>::iterator iter;

typename std::vector<T>::iterator iter;

Quote:

}

~StdVectorStorage()
{
}
};
Back to top
Sumit RAJAN
Guest





PostPosted: Tue Sep 19, 2006 9:10 am    Post subject: Re: creating a std::vector<T>::iterator ??? Reply with quote



Pierre Barbier de Reuille wrote:

Quote:
typedef std::vector<T>::iterator iter;

Are you sure you didn't mean typename std::vector<T>::iterator iter; ?

Sumit.
Back to top
Pierre Barbier de Reuille
Guest





PostPosted: Tue Sep 19, 2006 9:10 am    Post subject: Re: creating a std::vector<T>::iterator ??? Reply with quote

aaragon a écrit :
Quote:
I am trying to create a vector of type T and everything goes fine until
I try to iterate over it. For some reason, the compiler gives me an
error when I declare
std::vector<T>::iterator iter;
Any ideas why is tihs happening? The code is as follows:

template <class T
struct StdVectorStorage
{
std::vector<T>* _storage;

void create(size_t p,size_t l)
{
_storage = new std::vector<T>(p);

std::vector<T>::iterator iter;

}

~StdVectorStorage()
{
}
};


When using a type defined in a template, you have to use the keyword
"typename" so as to avoid any ambiguity:


typedef std::vector<T>::iterator iter;

Pierre
Back to top
F.J.K.
Guest





PostPosted: Tue Sep 19, 2006 9:10 am    Post subject: Re: creating a std::vector<T>::iterator ??? Reply with quote

aaragon wrote:
Quote:
std::vector<T>* _storage;

Just as an aside. All qualifiers beginning with an underscore are
reserved for the compiler + stdlib implementation. storage_ would be
much better.
Back to top
aaragon
Guest





PostPosted: Tue Sep 19, 2006 9:10 am    Post subject: Re: creating a std::vector<T>::iterator ??? Reply with quote

Sumit RAJAN wrote:
Quote:
Pierre Barbier de Reuille wrote:

typedef std::vector<T>::iterator iter;

Are you sure you didn't mean typename std::vector<T>::iterator iter; ?

Sumit.

It works now! I put

typename std::vector<T>::iterator it_;

Thank you guys for your help.

aa
Back to top
Pierre Barbier de Reuille
Guest





PostPosted: Tue Sep 19, 2006 9:10 am    Post subject: Re: creating a std::vector<T>::iterator ??? Reply with quote

Sumit RAJAN a écrit :
Quote:
Pierre Barbier de Reuille wrote:

typedef std::vector<T>::iterator iter;

Are you sure you didn't mean typename std::vector<T>::iterator iter; ?

Sumit.

Yes, sure!

Pierre
Back to top
Marco Wahl
Guest





PostPosted: Wed Sep 20, 2006 9:10 am    Post subject: Re: creating a std::vector<T>::iterator ??? Reply with quote

"aaragon" <alejandro.aragon (AT) gmail (DOT) com> writes:

Quote:
Thanks for your tips. I will be using thisNotation_ for variables from
now on.

Wonderful!

Quote:
... The following code:

#ifndef _POPULATION_H
#define _POPULATION_H

Ahrgh! Again there is a leading underscore! Remember F.K.J.s words:

Quote:
... qualifiers beginning with an underscore are
reserved for the compiler + stdlib
implementation.


Best wishes
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.