Incomplete type used in nested name specifier for Pimpl Idiom

I have this error for the following code

incomplete type 'Foo :: Pimpl used in the nested specifier

AnotherFoo.hpp

struct AnotherFoo {
    void methodAnotherFoo(Foo &);
};

AnotherFoo.cpp

#include "Foo.hpp"
#include "AnotherFoo.hpp"

void AnotherFoo::methodAnotherFoo(Foo &foo) {
    // here i want to save the function pointer of methodPimpl(), std::function for ex:
    std::function<void(void)> fn = std::bind(&Foo::Pimpl::methodPimpl, foo._pimpl); // <-- Here i am getting the error
}

Foo.hpp

struct Foo {
    Foo();
    class Pimpl;
    std::shared_ptr<Pimpl> _pimpl;
};

foo.cpp

#include "Foo.hpp"

struct Foo::Pimpl {
    void methodPimpl(void) {}    
};

Foo::Foo() : _pimpl(new Pimpl) {}

main.cpp

#include "Foo.hpp"
#include "AnotherFoo.hpp"

int main() {
    Foo foo;
    AnotherFoo anotherFoo;
    anotherFoo.methodAnotherFoo(foo);
}

Does anyone have a good solution how to fix this?

The main goal I'm trying to achieve is to keep the method signature methodAnotherFoohidden from header files.

+4
source share
4 answers

One solution I found is to move the Pimpl implementation to AnotherFoo.cpp

-1
source

The only file in which you can access the data Foo::Pimplis Foo.cpp, the file in which it is defined.

AnotherFoo.cpp.

:

  • AnotherFoo::methodAnotherFoo, Foo.

  • AnotherFoo::methodAnotherFoo Foo.cpp.

+4

AnotherFoo.cpp , , . , "detail/foo.h", .

0

Pimpl . , methodAnotherFoo. - :

class Foo
{
    public: Foo();

    public: void method(void);

    private: class Pimpl;
    private: std::shared_ptr<Pimpl> _pimpl;
};

// Foo.cpp
struct Foo::Pimpl
{
    void methodPimpl(void) {}    
};

Foo::Foo() : _pimpl(new Pimpl) {}

void Foo::method(void) {_pimpl->method();}

, -, :

void AnotherFoo::methodAnotherFoo(Foo &foo)
{
    std::function<void(void)> fn = std::bind(&Foo::method, foo);
}
0
source

Source: https://habr.com/ru/post/1690640/


All Articles