Reputation: 139
I want to return a nested class object using a friend function
template <typename T>
class X{
public:
class Y{
public:
int y;
}test;
public:
X(){
test.y=10;
}
template <typename U>
friend Y fun(X<U>);
};
template <typename T>
X<T>::Y fun(X<T> x){
return x.test;
}
But I get an error saying
need 'typename' before 'X::Y' because 'X' is a dependent scope
What is wrong?
Upvotes: 0
Views: 52
Reputation: 887
You have to write
template <typename T>
typename X<T>::Y fun(X<T> x){
return x.test;
}
becouse compliler can't resolve if Y
is filed or type.
Upvotes: 0
Reputation: 10336
You need to do literally what the error says: put typename
before X<T>::Y
:
template <typename T>
typename X<T>::Y fun(X<T> x){
return x.test;
}
Because the meaning of X<T>::Y
is type-dependent on T
, the compiler can't know in general whether X<T>::Y
refers to a typename or a variable. In such circumstances the rule is that if you want it to be typename, you must say so explicitly with the typename
keyword.
Upvotes: 1