可能重複:
Where and why do I have to put the 「template」 and 「typename」 keywords?調用模板函數
下面的代碼:
template<typename T>
class base
{
public:
virtual ~base();
template<typename F>
void foo()
{
std::cout << "base::foo<F>()" << std::endl;
}
};
template<typename T>
class derived : public base<T>
{
public:
void bar()
{
this->foo<int>(); // Compile error
}
};
而且,在運行時:
derived<bool> d;
d.bar();
我收到以下錯誤:
error: expected primary-expression before ‘int’
error: expected ‘;’ before ‘int’
我所知道的non-dependent names and 2-phase look-ups。但是,當函數本身是一個模板函數(我的代碼中的foo<>()
函數)時,我嘗試所有解決方法只是失敗。
謝謝!今天真的救了我的培根 – Jacko 2013-12-19 16:09:58