2011-04-01 17 views
5

İçiçe Şablonları: Benim içinBen C++ şablonları ile tek sorun var ve şu kod neden çalışmıyor anlamıyorum

$ g++ templates.cpp -o templates 
templates.cpp:14: error: expected ‘)’ before ‘f’ 
templates.cpp: In function ‘int main(int, char**)’: 
templates.cpp:23: error: no matching function for call to ‘Foo<TypePair<int, double> >::Foo(int, double)’ 
templates.cpp:12: note: candidates are: Foo<TypePair<int, double> >::Foo() 
templates.cpp:12: note:     Foo<TypePair<int, double> >::Foo(const Foo<TypePair<int, double> >&) 

:

#include <iostream> 

template <typename A, typename B> 
class TypePair { 
public: 
    typedef A First; 
    typedef B Second; 
}; 


template <typename P> 
class Foo { 
    public: 
     Foo(P::First f, P::Second) { 
      std::cout 
       << "first = " << f << std::endl 
       << "second = " << s << std::endl; 
     } 
}; 


int main(int argc, char **argv) { 
    Foo<TypePair<int, double> > foo(42, 23.0); 

    return 0; 
} 

kod aşağıdaki hataları üretir Bu kod tamamen iyi görünüyor, ama g ++ açıkçası kendi fikri vardır ^^ Herhangi bir fikir?

Sebastian

cevap

15

Kullanım

Foo(typename P::First f, typename P::Second s) 

P bir şablon parametresi P :: İlk ve P :: İkinci açıkça onlar için değil, typenames olan belirtmek zorunda bağımlı adları vardır olduğundan örnek, statik veri üyeleri. See this Detaylar için

+0

Hızlı cevabınız için çok teşekkür ederim! Sorunumu mükemmel bir şekilde çözdü. Güzel bir bağlantı için – Sebastian

+0

+1, teşekkürler. –