How do I forward declare an inner class?

C++Forward Declaration

C++ Problem Overview


I have a class like so...

class Container {
public:
    class Iterator {
        ...
    };

    ...
};

Elsewhere, I want to pass a Container::Iterator by reference, but I don't want to include the header file. If I try to forward declare the class, I get compile errors.

class Container::Iterator;

class Foo {
    void Read(Container::Iterator& it);
};

Compiling the above code gives...

test.h:3: error: ‘Iterator’ in class ‘Container’ does not name a type
test.h:5: error: variable or field ‘Foo’ declared void
test.h:5: error: incomplete type ‘Container’ used in nested name specifier
test.h:5: error: ‘it’ was not declared in this scope

How can I forward declare this class so I don't have to include the header file that declares the Iterator class?

C++ Solutions


Solution 1 - C++

This is simply not possible. You cannot forward declare a nested structure outside the container. You can only forward declare it within the container.

You'll need to do one of the following

  • Make the class non-nested
  • Change your declaration order so that the nested class is fully defined first
  • Create a common base class that can be both used in the function and implemented by the nested class.

Solution 2 - C++

I don't believe forward declaring inner class of on an incomplete class works (because without the class definition, there is no way of knowing if there actually is an inner class). So you'll have to include the definition of Container, with a forward declared inner class:

class Container {
public:
    class Iterator;
};

Then in a separate header, implement Container::Iterator:

class Container::Iterator {
};

Then #include only the container header (or not worry about forward declaring and just include both)

Solution 3 - C++

I know of no way to do exactly what you want, but here is a workaround, if you are willing to use templates:

// Foo.h  
struct Foo
{
   export template<class T> void Read(T it);
};

// Foo.cpp
#include "Foo.h"
#include "Container.h"
/*
struct Container
{
    struct Inner { };
};
*/
export template<> 
  void Foo::Read<Container::Inner>(Container::Inner& it)
{
  
}

#include "Foo.h"
int main()
{
  Foo f;
  Container::Inner i;
  f.Read(i);  // ok
  f.Read(3);  // error
}

Hopefully, this idiom might be of some use to you (and hopefully your compiler is EDG-based and implements export ;) ).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbradtgmurrayView Question on Stackoverflow
Solution 1 - C++JaredParView Answer on Stackoverflow
Solution 2 - C++Todd GardnerView Answer on Stackoverflow
Solution 3 - C++Faisal ValiView Answer on Stackoverflow