| View previous topic :: View next topic |
| Author |
Message |
aaragon Guest
|
Posted: Tue Sep 19, 2006 9:10 am Post subject: creating a std::vector<T>::iterator ??? |
|
|
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
|
Posted: Tue Sep 19, 2006 9:10 am Post subject: Re: creating a std::vector<T>::iterator ??? |
|
|
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
|
Posted: Tue Sep 19, 2006 9:10 am Post subject: Re: creating a std::vector<T>::iterator ??? |
|
|
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
|
Posted: Tue Sep 19, 2006 9:10 am Post subject: Re: creating a std::vector<T>::iterator ??? |
|
|
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
|
Posted: Tue Sep 19, 2006 9:10 am Post subject: Re: creating a std::vector<T>::iterator ??? |
|
|
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
|
Posted: Tue Sep 19, 2006 9:10 am Post subject: Re: creating a std::vector<T>::iterator ??? |
|
|
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
|
Posted: Tue Sep 19, 2006 9:10 am Post subject: Re: creating a std::vector<T>::iterator ??? |
|
|
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
|
Posted: Wed Sep 20, 2006 9:10 am Post subject: Re: creating a std::vector<T>::iterator ??? |
|
|
"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 |
|
 |
|