UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » Problem with template class and std::list

#1 2009-06-22 01:56 PM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: Problem with template class and std::list

Hello!

I can't figure out why this code doesn't work?

#include <list>

template <class T> class Foo 
{
  public:
      std::list<T>::iterator iter;
      
  private:
      std::list<T> elements;
};

int main (void)
{
    return 0;
}

This is g++ output:
kod.cpp:6: error: type ‘std::list<T, std::allocator<_Tp1> >’ is not derived from type ‘Foo<T>’
kod.cpp:6: error: expected ‘;’ before ‘iter’

Can someone explain it for me please?

Offline

#2 2009-06-24 02:12 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Problem with template class and std::list

std::list<T>::iterator - is dependent name
you should prefix this with typename.

Offline

#3 2009-06-24 03:06 PM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: Problem with template class and std::list

ok, but what exactly keyword 'typename' does?

Offline

#4 2009-06-24 04:19 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Problem with template class and std::list

"typename" - Tells the compiler that an unknown identifier is a type.
Names that depend in some way on the template arguments, referred to as dependent names

(c) MSDN

Offline

#5 2009-06-29 12:55 AM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: Problem with template class and std::list

One more question. I noticed problem while implementing template class. Everything compiles and work fine when I put both tmeplate's declaration and implementation in header file. Linker fails when I leave declaration in header and move implementation to source file.

Is it normal behavior...?

Offline

#6 2009-06-29 11:51 PM

Uzume
Administrator
Registered: 2002-08-30
Posts: 186

Re: Problem with template class and std::list

I don't use C++ and templates much but it seems to me they would have to work in a fashion similar to inline code and as such would have to be in a header file and not a normal source file (unless that is the also the only place it is used) as otherwise there is no way to know which declarations to generate from the templates until one sees the instantiations (which cannot be done unless it is in a header file that is included in all source files that use the a template instantiation).

Offline

  • Index
  • » C++
  • » Problem with template class and std::list

Board footer

Powered by FluxBB