gpt4 book ai didi

c++ - Pimpl Idiom 的嵌套名称说明符中使用的不完整类型

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:59:38 26 4
gpt4 key购买 nike

我有以下代码的错误

incomplete type ‘Foo::Pimpl’ used in nested name specifier

另一个Foo.hpp

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

另一个Foo.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) {}

主要.cpp

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

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

有没有人有好的解决方案来解决这个问题?

我试图实现的主要目标是将 methodAnotherFoo 方法的签名隐藏在头文件中。

最佳答案

您可以在其中访问 Foo::Pimpl 详细信息的唯一文件是 Foo.cpp,即定义它的文件。

您不能在 AnotherFoo.cpp 中访问它。

您的选择是:

  1. 更改 AnotherFoo::methodAnotherFoo 的实现以仅使用 Foo 的公共(public)接口(interface)。

  2. AnotherFoo::methodAnotherFoo 的实现移动到 Foo.cpp。

关于c++ - Pimpl Idiom 的嵌套名称说明符中使用的不完整类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47781790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com