使用unique_ptr作为不透明的对象ID
以下操作来隐藏我项目中第三方C ++图库(柠檬图库)的实现详细信息:
api.h file
class Node;
using NodeId = unique_ptr<Node>;
class ProgramGraph {
private:
class ProgramGraphImpl;
ProgramGraphImpl* pimpl;
public:
ProgramGraph();
NodeId add_instr_node(int opcode, const string& desc, BlockId& parent);
...
};
api.cpp文件
class Node {
public:
ListDigraph::Node node;
Node(ListDigraph::Node node) : node(node) {}
~Node() {}
};
class ProgramGraphImpl {
NodeId add_instr_node(int opcode, const string& desc, BlockId& parent) {
ListDigraph::Node n = pg.addNode(); // this is from the 3rd party library
unique_ptr<Node> uq = make_unique<Node>(Node(n));
node_map[n] = Instr(opcode, desc, parent); // Instr is another class defined in API.cpp
return uq;
}
...
};
NodeId ProgramGraph::add_instr_node(int opcode, const string& desc, BlockId& parent) {
return pimpl->add_instr_node(opcode, desc, parent);
}
我在some_other_other_file.cpp中使用这样的api。
#include "API.h"
...
const NodeId& currMI = pg.add_instr_node(opcode, opcode_desc, block_id);
...
我通过执行 ,我在下面收到错误(错误:“ sizeof”的应用程序不完整的类型program_graph :: node
) - 有什么想法吗?谢谢。
/applications/xcode.app/contents/developer/platforms/macosx.platform/develform/sdks/macosx12.3.3.sdk/usr/incl/include/c ++ ++/v1/v1/v1/__ norement/soruse/simory_ptr.h:53:19:19:53:53:19::53:53:19::19::53:19::53:19 :: 53:53:19: 错误:将“ sizeof”应用于不完整类型的无效 'program_graph ::节点' static_assert(sizeof(_tp)&gt; 0, ^~~~~~~~~~~~ H:318:7: 注意:成员功能的实例化 'std :: default_delete&lt; program_graph :: node&gt; :: operator()' 在这里要求 _ ptr .second()(__ tmp); ^/applications/xcode.app/contents/developer/platforms/macosx.platform/developer/sdks/macosx12.3.sdk/usr/include/include/c+++++++++++++++++++++++++spr./__memory/unique_ptr.h:272:19:272:19:272:19:19: 注意:成员功能的实例化 'std :: simolor_ptr&lt; program_graph :: node&gt; :: reset'请求 这里〜unique_ptr(){reset(); } ^ some_other_file.cpp:151:36: 注意:成员功能的实例化 'std :: unique_ptr&lt; program_graph :: node&gt; :: 〜simel_ptr' 在这里要求 const nodeid&amp; currmi = pg.add_instr_node(opcode,opcode_desc,block_id);
I'm hiding the implementation details of third party C++ graph library (LEMON graph library) from my project by doing the following :
API.h file
class Node;
using NodeId = unique_ptr<Node>;
class ProgramGraph {
private:
class ProgramGraphImpl;
ProgramGraphImpl* pimpl;
public:
ProgramGraph();
NodeId add_instr_node(int opcode, const string& desc, BlockId& parent);
...
};
API.cpp file
class Node {
public:
ListDigraph::Node node;
Node(ListDigraph::Node node) : node(node) {}
~Node() {}
};
class ProgramGraphImpl {
NodeId add_instr_node(int opcode, const string& desc, BlockId& parent) {
ListDigraph::Node n = pg.addNode(); // this is from the 3rd party library
unique_ptr<Node> uq = make_unique<Node>(Node(n));
node_map[n] = Instr(opcode, desc, parent); // Instr is another class defined in API.cpp
return uq;
}
...
};
NodeId ProgramGraph::add_instr_node(int opcode, const string& desc, BlockId& parent) {
return pimpl->add_instr_node(opcode, desc, parent);
}
I am using the API like this in some_other_file.cpp :
#include "API.h"
...
const NodeId& currMI = pg.add_instr_node(opcode, opcode_desc, block_id);
...
When I compile it, I get the error below (error: invalid application of 'sizeof' to an incomplete type program_graph::Node
) - any ideas ? Thank you.
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__memory/unique_ptr.h:53:19:
error: invalid application of 'sizeof' to an incomplete type
'program_graph::Node'
static_assert(sizeof(_Tp) > 0,
^~~~~~~~~~~ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__memory/unique_ptr.h:318:7:
note: in instantiation of member function
'std::default_delete<program_graph::Node>::operator()'
requested here
_ptr.second()(__tmp);
^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__memory/unique_ptr.h:272:19:
note: in instantiation of member function
'std::unique_ptr<program_graph::Node>::reset' requested
here ~unique_ptr() { reset(); }
^ some_other_file.cpp:151:36:
note: in instantiation of member function
'std::unique_ptr<program_graph::Node>::~unique_ptr'
requested here
const NodeId& currMI = pg.add_instr_node(opcode, opcode_desc, block_id);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是重新定义类
programGraph
在“ api.cpp”文件中。尝试将其删除,重新编译并提供您的代码的更详细版本。
只是简单:
You was redefinition class
ProgramGraph
in "API.cpp" file.Try removing it, and recompiling and provide a more detailed version of your code.
Just simple:
需要使用类似的自定义eleter:
然后在.cpp文件中提供exter:
因为默认的deleter对不完整类型不起作用。
Need to use a custom deleter something like:
and then providing the deleter in the .cpp file:
since the default deleter does not work on incomplete types.