I've searched through the web an didn't find any explanation why the following happens.
For example, there is a template class Enclosing with a nested class Nested.
In Enclosing class, there is a method that should create an instance of the Nested class and use it's fields and methods.
In the following code, there is a model of how I'm trying to do it:
template<typename T, typename S>
class Enclosing{
public:
class Nested;
Nested foo();
};
template<typename T, typename S>
class Enclosing<T,S>::Nested{
public:
T field;
void some_method();
friend class Enclosing; // instead of this line I also tried:
// friend class Enclosing<T,S>
// and it didn't work either
};
template<typename T, typename S>
typename Enclosing<T,S>::Nested Enclosing<T,S>::foo (){
Nested nes;
nes.some_method; // the problem appears here
return enc;
}
The problem is:
When I'm writing nes.some_method
, none of the environments I tried (VS2010, eclipse), after I type "nes.", doesn't propose me any options. I seems like "nes" is not an instanse of the class at all.
How to access methods and fields of the nested class from enclosing template class?
This line:
Nested nes();
Does not create an object of type nes
, but rather declares a function which takes no arguments and returns an object of type Nested
. I suspect this to be the source of your problem, not the friend
declaration. Just remove the pair of parentheses after nes
:
Nested nes;
Alternatively, in C++11 you may do:
Nested nes{};
EDIT:
After fixing the above error, it still seems your program does not compile & link correctly - doubtfully because of the same problem. What I can tell from your code is that a definition of the some_method()
member function is still missing, and that may be a reason for the linker to refuse creating the executable for your program.