LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2005, 05:21 PM   #1
Nicholas Bishop
LQ Newbie
 
Registered: Feb 2005
Posts: 4

Rep: Reputation: 0
Template class with a template member...


I am trying to create a template class that passes its template down to a member that is also a template class, as in this example:
Code:
#include <list>

template< typename T > class Foo
{
public:
        Foo()
        {
                std::list< T >::iterator i;

                // Do stuff
        }
private:
        std::list< T > bar;
};
Compiling (with g++ 3.4.1) gives this error:
vu.cc: In constructor `Foo<T>::Foo()':
vu.cc:8: error: expected `;' before "i"

What is the correct way to do something like this?
 
Old 02-21-2005, 05:42 PM   #2
Nicholas Bishop
LQ Newbie
 
Registered: Feb 2005
Posts: 4

Original Poster
Rep: Reputation: 0
Well, I think I've found a solution:

Code:
#include <list>

template< typename T > class Foo
{
public:
        Foo()
        {
                typename std::list< T >::iterator i;
                for( i = bar.begin(); i != bar.end(); i++ )
                {
                        // Do stuff
                }
        }
private:
        std::list< T > bar;
};
But I don't understand why this is necessary...

Last edited by Nicholas Bishop; 02-21-2005 at 08:35 PM.
 
Old 02-21-2005, 06:57 PM   #3
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Because the compiler doesn't know whether ::iterator is a type or a value, so you need to give it a helping hand.

Code:
template <typename T>
struct some_class
{
    typedef T value_type;  // a type
    static T  value;   // a value
};

template<typename U>
void some_func(U u)
{
    U::value_type v = U::value;      // confused
    typename U::value_type w = U::value; // better
}
I don't have 3.4.1 to hand but didn't you get some warnings like "implicit typename is deprecated"?

HTH
 
Old 02-21-2005, 08:27 PM   #4
Nicholas Bishop
LQ Newbie
 
Registered: Feb 2005
Posts: 4

Original Poster
Rep: Reputation: 0
No, I didn't get any other messages from the compiler.

Thanks for the explanation, it makes a lot more sense now.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ class template doesn't work!! tagigogo Programming 2 11-02-2005 04:45 PM
function prototypes in template class' (c++) qwijibow Programming 4 12-13-2004 09:34 AM
template class ckcheung0927 Programming 3 11-28-2004 03:59 PM
C++ - class template - linker errors? Wynd Programming 2 10-19-2004 03:54 PM
C++ template class syntax rong Programming 12 08-13-2003 12:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration