Reputation: 127
I am using c++ templates and created a template class inside a template class.
the situation is like this:
template <typename T>
class C {
public:
class N {
T v;
};
template <typename D>
N *fun(D d);
};
template <typename T>
template <typename D>
N *C<T>::fun(D d) {
}
int main() {
C<int> obj;
obj.fun('c');
}
Now compiler is giving error as:
main.cpp:14:1: error: ‘N’ does not name a type
If I use the function prototype outside class as
C<T>::N *C<T>::fun(D d)
, compiler gives error:
main.cpp:14:1: error: need ‘typename’ before ‘C::N’ because ‘C’ is a dependent scope
If I define definition inside the class then it works fine. But I don't want to make it inline, how should I do it?
Upvotes: 4
Views: 533
Reputation: 1334
As the compile error suggests, use typename
template <typename T>
template <typename D>
typename C<T>::N *C<T>::fun(D d) {
}
Upvotes: 8
Reputation: 217265
As alternative, you might use trailing return type:
template <typename T>
template <typename D>
auto C<T>::fun(D d) -> N*
{
return nullptr;
}
Upvotes: 2