4

I am developing a library and a would like to provide my users a public interface separate from the real implementation that is hidden in a namespace. This way, I could change only the class HiddenQueue without changing myQueue that will be exposed to users only.

If I put the C++ code of HiddenQueue in the myQueue.cpp file the compiler complains saying _innerQueue has incomplete type. I thought that the linker was able to resolve this. What I am doing wrong here?

// myQueue.h
namespace inner{
    class HiddenQueue;
};

class myQueue{

public:
    myQueue();
);

private:

    inner::HiddenQueue _innerQueue;

};

///////////////////////////

// myQueue.cpp
namespace inner{
    class HiddenQueue{};
};
manlio
  • 16,658
  • 13
  • 67
  • 107
Abruzzo Forte e Gentile
  • 13,055
  • 24
  • 87
  • 163

3 Answers3

6

The compiler needs to know the exact memory layout of an object by looking at the header file it's defined in.

Your code says that class MyQueue has a member of type InnerQueue, which will be part of the memory layout of MyQueue objects. Therefore, to deduce the memory layout of MyQueue it needs to know the memory layout of InnerQueue. Which it does not, because you say "oh well, it's defined elsewhere".

What you are trying to do is closely related to "the PIMPL idiom"/"compiler firewall" technique.

To solve the problem, you must either include HiddenQueue.h in your header or declare _innerqueue as a pointer:

class myQueue {

public:
    myQueue();

private:

    inner::HiddenQueue* _pinnerQueue;
};

Using a pointer is possible because a pointer has a known memory size (dependent on your target architecture), therefore the compiler doesn't need to see the full declaration of HiddenQueue.

Community
  • 1
  • 1
Jon
  • 396,160
  • 71
  • 697
  • 768
  • +1 For PIMPL. Here's an article from the guy who coined the term: http://www.gotw.ca/gotw/024.htm – Emile Cormier May 11 '10 at 14:41
  • Well, in the bottom line PIMPL incurs some runtime speed cost and offers you gains on ease of maintenance and compilation speed. Admittedly the runtime speed cost is a non-issue in the general case (and when it isn't, you probably know it's important), but IMHO there's no need for PIMPL in codebases less than a certain size. – Jon May 11 '10 at 14:46
1

To be able to make a member of a class, you need to have a definition for it and not only a declaration. (A declaration is enough for a pointer or a reference to the class).

AProgrammer
  • 48,232
  • 8
  • 83
  • 139
1

You need to provide pointer to _innetQueue rather then the object itself:

std::auto_ptr<inner::HiddenQueue> _innerQueue;

Search form pimpl ideom or d-pointer

Artyom
  • 30,091
  • 20
  • 121
  • 208
  • Don't use `auto_ptr` for the Pimpl idiom, its weird copy/assignment semantics are more trouble that they're worth. `unique_ptr` or `scoped_ptr` are much better, and you can just code a very simple class if necessary. – Matthieu M. May 11 '10 at 16:48
  • `unique_ptr` comes only with C++0x, `scoped_ptr` require boost. `auto_ptr` exists anywhere. Also when your class is not copyable it is more then fine to use `auto_ptr` -- in other words: you need smart pointer and standard C++ provides only `auto_ptr` – Artyom May 11 '10 at 19:08