 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jason Carucci Guest
|
Posted: Wed Dec 10, 2003 8:35 pm Post subject: What is wrong with this class? |
|
|
Here's a little test program where I define a class that derives from
an iterator. The first commented out version fails to compile. The
error I get from Visual C++ 7.1 is the following:
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(7) :
warning C4346: 'T::value_type' : dependent name is not a type
prefix with 'typename' to indicate a type
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(10)
: see reference to class template instantiation 'Test1<T>' being
compiled
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(7) :
error C2923: 'std::iterator' : 'T::value_type' is invalid as template
argument '#2', type expected
The second version does compile, I can't figure out the difference
between them.
#include <iterator>
#include <string>
/* Version 1, This fails to compile
template<class T>
class Test1
: public std::iterator<std::output_iterator_tag, T::value_type >
{
public:
};
*/
// Version 2, this does compile
template<class T>
class Test1
: public std::iterator<std::output_iterator_tag, wchar_t >
{
public:
};
int main(int argc, char* argv[])
{
Test1<std::wstring> testing;
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Hansen Guest
|
Posted: Thu Dec 11, 2003 10:48 am Post subject: Re: What is wrong with this class? |
|
|
On 10 Dec 2003 15:35:52 -0500, there came a drop of sanity from
[email]jcarucci (AT) yahoo (DOT) com[/email] (Jason Carucci) containing:
| Quote: | Here's a little test program where I define a class that derives from
an iterator. The first commented out version fails to compile. The
error I get from Visual C++ 7.1 is the following:
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(7) :
warning C4346: 'T::value_type' : dependent name is not a type
prefix with 'typename' to indicate a type
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(10)
: see reference to class template instantiation 'Test1<T>' being
compiled
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(7) :
error C2923: 'std::iterator' : 'T::value_type' is invalid as template
argument '#2', type expected
The second version does compile, I can't figure out the difference
between them.
#include <iterator
#include
/* Version 1, This fails to compile
template
class Test1
: public std::iterator
{
public:
};
*/
// Version 2, this does compile
template
class Test1
: public std::iterator
{
public:
};
int main(int argc, char* argv[])
{
Test1
return 0;
}
|
Well, the error says it all...!
Change inheritance to:
public
std::iterator<std::output_iterator_tag, typename T::value_type>
Reason is that the compiler can't possible know at compile time if
this is a type or a value!
You clearify things by adding the "typename" keyword...
I have just gone through a similar problem in my library SmartWin when
porting to GCC!
Reason to problem is that MSVC++7.1 doesn't require the "typename"
keyword unless it HAVE to have it, so us Microsoft C++ coders aren't
really that used to the keyword!
--
http://smartwin.sourceforge.net THE template based Windows API GUI Library
My email:
"john.johnson (AT) theJohnsons (DOT) com"
* Replace "john" with "thomas", replace "johnson" with "hansen", replace "theJohnsons" with "adramatch"
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Thu Dec 11, 2003 11:44 am Post subject: Re: What is wrong with this class? |
|
|
Jason Carucci wrote:
| Quote: | Here's a little test program where I define a class that derives from
an iterator. The first commented out version fails to compile. The
error I get from Visual C++ 7.1 is the following:
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(7) :
warning C4346: 'T::value_type' : dependent name is not a type
prefix with 'typename' to indicate a type
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(10)
: see reference to class template instantiation 'Test1<T>' being
compiled
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(7) :
error C2923: 'std::iterator' : 'T::value_type' is invalid as template
argument '#2', type expected
|
GCC 3.3.1 gives this error message...
typename.cpp:8: error: to refer to a type member of a template
parameter, use
`typename T::value_type'
| Quote: |
The second version does compile, I can't figure out the difference
between them.
#include <iterator
#include
/* Version 1, This fails to compile
template
class Test1
: public std::iterator
|
try this ------ std::iterator
T::value_type >
There exists lots of history about why you need to use typename.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler (nee Spang Guest
|
Posted: Thu Dec 11, 2003 12:06 pm Post subject: Re: What is wrong with this class? |
|
|
Good Morning, Jason Carucci!
Jason Carucci schrieb:
| Quote: | Here's a little test program where I define a class that derives from
an iterator. The first commented out version fails to compile. The
error I get from Visual C++ 7.1 is the following:
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(7) :
warning C4346: 'T::value_type' : dependent name is not a type
prefix with 'typename' to indicate a type
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(10)
: see reference to class template instantiation 'Test1<T>' being
compiled
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(7) :
error C2923: 'std::iterator' : 'T::value_type' is invalid as template
argument '#2', type expected
The second version does compile, I can't figure out the difference
between them.
#include <iterator
#include
/* Version 1, This fails to compile
template
class Test1
: public std::iterator
|
Missing typename in front of T::value_type:
template
class Test1
: public std::iterator<std::output_iterator_tag, typename T::value_type >
{
};
Hope that helps,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mike Capp Guest
|
Posted: Thu Dec 11, 2003 8:43 pm Post subject: Re: What is wrong with this class? |
|
|
[email]jcarucci (AT) yahoo (DOT) com[/email] (Jason Carucci) wrote in message news:<58c726b1.0312100933.721b0cde (AT) posting (DOT) google.com>...
| Quote: | /* Version 1, This fails to compile
template<class T
class Test1 : public std::iterator
|
The compiler has no way of knowing that T::value_type is a type as
opposed to, say, a static data member. Just tell it explicitly using
the "typename" keyword, i.e.
template
class Test1 : public std::iterator<std::output_iterator_tag, typename
T::value_type>
and you should be fine.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gareth Stockwell Guest
|
Posted: Thu Dec 11, 2003 9:09 pm Post subject: Re: What is wrong with this class? |
|
|
Jason,
The difference is that, in the first version, the type of
'T::value_type' is dependent on the template parameter T. So the base
class should be written using the 'typename' keyword, like this:
public std::iterator<std::output_iterator_tag, typename T::value_type>
Gareth
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Rob Guest
|
Posted: Mon Dec 15, 2003 10:20 am Post subject: Re: What is wrong with this class? |
|
|
"Jason Carucci" <jcarucci (AT) yahoo (DOT) com> wrote
| Quote: | Here's a little test program where I define a class that derives from
an iterator. The first commented out version fails to compile. The
error I get from Visual C++ 7.1 is the following:
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(7) :
warning C4346: 'T::value_type' : dependent name is not a type
prefix with 'typename' to indicate a type
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(10)
: see reference to class template instantiation 'Test1<T>' being
compiled
c:VSS_RDCConversionExamplesIteratorExampleIteratorExample.cpp(7) :
error C2923: 'std::iterator' : 'T::value_type' is invalid as template
argument '#2', type expected
The second version does compile, I can't figure out the difference
between them.
#include <iterator
#include
/* Version 1, This fails to compile
template
class Test1
: public std::iterator
{
public:
};
*/
|
As the error message said, what you need to do is:
class Test1
: public std::iterator
// etc
[second example using wchar_t instead of T::value_type snipped].
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|