Click here to Skip to main content
13,054,615 members (47,297 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 4 Mar 2013

Forward Declaration and a Pointer

, 4 Mar 2013
Rate this:
Please Sign up or sign in to vote.
Why forward declaration can cause some troubles with (smart) pointers.

Mister C was a cool guy. C could make amazing things with just ordinary text files. He used to grab a bunch of them and produce magic binary forms. He could make a spinning cube, web server, or even an operating system.

One time he was running through a plain header file. The day was calm and nothing portended a change. Suddenly mister C noticed one interesting line in the file.

class MyType;

"Cool" - he thought - "a forward declared type". After several other rather common statements that looked similar to this:

class Test
    Test() { }
    ~Test() { }
    void doAll() { }

He focused on another piece of code:

    std::unique_ptr<MyType> m_myType; 

After that line, everything changed for mister C. He was so amazed by the code that all he could do was to output a nasty error message.

Do you know what the message was? Why did he do that?

The Message

In the output window (Visual Studio), there can be something like:

... while compiling class template member function
'void std::default_delete&lt;_Ty&gt;::operator ()(_Ty *) throw() const'

Or in the error list page:

error C2338: can't delete an incomplete type


Poor mister C had simply no idea how to delete object inside unique pointer. The deletion should happen in the destructor of the class MyTest of course. But since it was in the header file, the problem arose.

Although mister C appreciated authors' suggestion about forward declared type, his policy told him that at that point, there has to be full type definition. Unique pointer required that, to be more specific, its static deleter has to be defined properly.


Mister C cannot find a proper solution, this is our task. To help him, we can just move implementation of the destructor to some source file where the type MyType will be completely known. Simple as it is.

Another option is to use shared_ptr instead. Although it is a bit counter intuitive, the reason for that is simple. Shared pointer has dynamic deleter object that is selected at runtime, so there will be no errors at compile time.

Note that when we create some object, we need to know full type definition of course.

Look below for more information about improving communication with you and mister C:

This post is just a quick addition to my previous post about Smart Pointer Gotchas.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bartlomiej Filipek
Software Developer
Poland Poland
Software developer interested in creating great code and passionate about teaching.

I have around 10 years of professional experience in C++/Windows/Visual Studio programming. Plus other technologies like: OpenGL, game development, performance optimization.

If you like my articles please subscribe to my coding blog or just visit

You may also be interested in...

Comments and Discussions

QuestionForward Declaration makes no sense for header only! Pin
pip01015-Feb-16 0:41
memberpip01015-Feb-16 0:41 
Questionstd::auto_ptr ? Pin
emptyy228-Mar-14 7:08
memberemptyy228-Mar-14 7:08 
AnswerRe: std::auto_ptr ? Pin
Bartlomiej Filipek30-Mar-14 20:51
memberBartlomiej Filipek30-Mar-14 20:51 
QuestionImplementation Hiding with C++11 unique_ptr and shared_ptr Pin
George L. Jackson8-Mar-13 8:39
memberGeorge L. Jackson8-Mar-13 8:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 4 Mar 2013
Article Copyright 2013 by Bartlomiej Filipek
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid