views:

112

answers:

3

Hi all

I am developing a library and a would like to provide my users a public interface separate from the real implementatino 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?

Thanks

Afg

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

class myQueue{

public:
    myQueue();
);

private:

    inner::HiddenQueue _innerQueue;

};

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

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

A: 

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
+5  A: 

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.

Jon
+1 For PIMPL. Here's an article from the guy who coined the term: http://www.gotw.ca/gotw/024.htm
Emile Cormier
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
+1  A: 

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
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.
`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