如何在堆栈 c++ 中存储 void*容器
我正在使用用 C 编写的库,并且该库提供仅使用 void* 的标头。该库用于创建一种图形,存储在 C 数据库中。标头将 void* 返回到图中的节点。为了创建图表,我需要解析一堆节点名称。与节点名称堆栈并行,我需要为节点维护一个 void* 堆栈。我有这样的事情:
std::stack < void* > nodeStack;
while (!nodeNameStack.empty()) {
// check if nodeNamestack.front() meets some criteria
nodeStack.push(C_API_To_Create_Node(nodeNameStack.pop()));
// Do some processing
// check if nodeStack.size() >= 2
void *node1 = nodeStack.pop()
void *node2 = nodeStack.pop()
// Above line issues error saying void value not ignored as it ought to be..
我不确定问题是什么,因为我们保证nodeStack大小至少为2。如果有任何建议来克服这个错误,我将不胜感激。
I'm using a library written in C and the library provides headers which only use void*. The library is used to create a kind of graph, which is stored inside the C data-base. The headers return void* to the nodes in the graph. To create the graph, I need to parse a stack of lets say node names. In parallel to the stack of node names, I need to maintain a stack void* for the nodes. I have something like this:
std::stack < void* > nodeStack;
while (!nodeNameStack.empty()) {
// check if nodeNamestack.front() meets some criteria
nodeStack.push(C_API_To_Create_Node(nodeNameStack.pop()));
// Do some processing
// check if nodeStack.size() >= 2
void *node1 = nodeStack.pop()
void *node2 = nodeStack.pop()
// Above line issues error saying void value not ignored as it ought to be..
I'm not sure what the issue is, as we guarantee nodeStack size is atleast 2. I would appreciate any suggestions to overcome this error..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
stack::pop 的原型是
因此它不会返回任何内容,因此警告。也许您打算使用 stack::top
The prototype for stack::pop is
Therefore it does not return anything hence the warning. Perhaps you meant to use stack::top
std::stack::pop()
不会返回被删除的元素。在弹出它之前,您必须使用top()
读取它。std::stack::pop()
doesn't return the element removed. You have to read it withtop()
before popping it.2 件事
A) 你忘记了 ; 在 nodeStack.pop() 之后。
B) .pop() 返回 void 这就是您收到错误的原因。 .pop() 只是从堆栈中删除元素。使用 .top() 获取元素,然后使用 .pop() 删除它。
2 Things
A) you forgot ;'s after nodeStack.pop().
B) .pop() returns void which is why you are getting the error. .pop() just removes the element from the stack. Use .top() to get the element, then .pop() to remove it.