Compile error on class template

S

soft wind

I have a compile error in my code:

-------------------------------------------------
#include <vector>

template < typename T >
class Foo {
public:
typedef std::vector< T >::iterator iterator; // (1)error (I
want to fix this)
// typedef std::vector< typename T >::iterator iterator; // (2)
still error

typedef T::const_iterator const_iterator; // (3) error
// typedef typename T::const_iterator const_iterator; // (4)OK

private:
std::vector< T > vec_m;
};

int main( void )
{
return 0;
}
-------------------------------------------------

Compiler : gcc version 4.0.1
Error message : type 'std::vector<T, std::allocator<_CharT> >' is not
derived from type 'Foo<T>'

In case of "T::const_iterator "(3) , I can fix it by adding
"typename" (4)
but similar way to fix is not effective for (1).

How can I fix (1) ?

Thanks,

Tsunehiko
 
V

Victor Bazarov

soft said:
I have a compile error in my code:

-------------------------------------------------
#include <vector>

template < typename T >
class Foo {
public:
typedef std::vector< T >::iterator iterator; // (1)error (I
want to fix this)
// typedef std::vector< typename T >::iterator iterator; // (2)
still error

typedef T::const_iterator const_iterator; // (3) error
// typedef typename T::const_iterator const_iterator; // (4)OK

private:
std::vector< T > vec_m;
};

int main( void )
{
return 0;
}
-------------------------------------------------

Compiler : gcc version 4.0.1
Error message : type 'std::vector<T, std::allocator<_CharT> >' is not
derived from type 'Foo<T>'

In case of "T::const_iterator "(3) , I can fix it by adding
"typename" (4)
but similar way to fix is not effective for (1).

How can I fix (1) ?

Search the FAQ for "dependent name".

V
 
S

soft wind

Thank you.

I find a way to fix it by searching gcc *.h files by "typedef" &
"::iterator" .

typedef std::vector< T >::iterator iterator; // error
typedef typename std::vector< T >::iterator iterator; // OK

Grammar for "typename" in my case is:
typename nested-name-specifier identifier

ex.
typename template-id :: identifier

Is this right understanding ?

Tsunehiko
 
V

Victor Bazarov

soft said:
I find a way to fix it by searching gcc *.h files by "typedef" &
"::iterator" .

typedef std::vector< T >::iterator iterator; // error
typedef typename std::vector< T >::iterator iterator; // OK

Grammar for "typename" in my case is:
typename nested-name-specifier identifier

ex.
typename template-id :: identifier

Is this right understanding ?

I suppose. Still, try to find the time to read the FAQ or the archives
about the "dependent name". Why is 'typename' required? You will find
the answers in the FAQ, I believe.

V
 
S

soft wind

Victor Bazarov said:
 Why is 'typename' required?  

Without 'typename', compiler think 'std::vector< T >' is identifier,
not class because 'std::vector< T >' is nondependent name.
Right ?

Tsunehiko
 
V

Victor Bazarov

soft said:
Without 'typename', compiler think 'std::vector< T >' is identifier,
not class because 'std::vector< T >' is nondependent name.
Right ?

No. It knows that 'std::vector<T>' is a class because that's how
'std::vector' is defined. What it can't know is what's inside of that
class because 'T' is yet to be determined. So, 'std::vector<T>::blah'
(which is totally dependent on what 'T' is) is something that it would
assume a *value* unless you insist on its being a type. So, to tell the
compiler that 'std::vector<T>::iterator' is a type you supply 'typename'.

Is FAQ really that unclear?

V
 
S

soft wind

Victor Bazarov said:
Is FAQ really that unclear?

The idea in C++ FAQ Lite [35.18] which you contributed is clear but
my statement was misleading. By what you explain this time,
I notice that my statement was wrong.

I knew that compiler does not know 'T' is type. So at first
I wrote 'std::vector< typename T >::iterator' but the position of
'typename' was wrong.

I apologize my reading of the FAQ was not enough. I find
typename B<T>::Xyz x; ↠good
in the FAQ, which is applicable to my case.

Tsunehiko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top