std::函数中的类型不完整
Incomplete type in std::function
我有一个类似于以下内容的Target
类:
class Target
{
std::function<void(A&,B&,C&)> Function;
}
现在,其中一个参数类型(比如A)有一个Target
成员,并试图调用它的函数:
class A
{
Target target;
void Foo(B& b, C& c)
{
target.Function(*this,b,c);
}
}
在后面的某个地方,这两种类型出现在头文件中。考虑到循环依赖关系,有一个正向声明,不幸的是,还有一个error : pointer to incomplete class type is not allowed
错误。
所以问题是,我该怎么办?
您有一个循环依赖性问题。将target
声明为class A
中的指针,并在构造函数中适当地分配它,并在类的析构函数中取消分配它:
class A
{
A() : target(new Target) {}
~A() { delete target; }
Target *target;
void Foo(B &b, C &c)
{
target->Function(*this, b, c);
}
};
如果您的编译器支持C++11,请改用智能指针:
class A
{
A() : target(std::unique_ptr<Target>(new Target)) {}
std::unique_ptr<Target> target;
void Foo(B &b, C &c)
{
(*target).Function(*this, b, c);
}
};
相关文章:
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 将C++子类成员函数(虚拟实现)传递给 C 类型函数指针
- C++ 这里有一个返回 (24) 的布尔返回类型函数
- 使用SFINAE来检测void返回类型函数的存在
- 使用此类型函数有什么优势
- 为什么此函数通过类型函数指针调用后,呼叫明智地行为
- 如何使用无类型函数指针调用C++成员函数
- 模板返回类型函数如何在C++中工作
- 具有通用类型函数的动态库[C ]
- 如何在返回类型函数模板的专用化中使用派生类型?( "couldn't infer template argument" )
- Bon appetit :从 int 返回类型函数在 main() 中打印字符串
- 对于需要其他模板参数的类型函数的部分模板专业化
- c++错误的参数类型-函数指针
- 延迟评估模板类型函数
- 在引用或指针返回类型函数上输入
- 具有指针数据类型的非类型函数模板参数
- STL中使用的C++自定义比较类型(函数谓词与较少结构)
- C++模板基类的非类型函数模板的 using 声明
- 字符串到类型函数,模板专用化使调用统一
- 自由类型函数可以接受 Unicode 文件名吗?