【发布时间】:2019-09-15 20:28:18
【问题描述】:
我正在编写一组具有特定要求的链表函数。我必须编写一个具有以下要求的函数
string retrieve_front (llheader)
retrieves the contents of the node at the front. Does not remove the node. If the list is empty, throws an exception.
用这个结构定义
struct LLnode
{
LLnode * fwdPtr; // has a pointer member
string theData; // the data within the node
};
我真的想返回第一个节点的十六进制地址以及其中的数据,我现在想知道这是可能的
string retrieve_front(LLnode * theLLHeader)
{
string data, report;
if (theLLHeader == nullptr)
{
throw "This list is empty";
}
else
{
data = theLLHeader -> theData;
report = "Front_Node[address: " + theLLHeader + " data: " + data + "]\n";
return report;
}
}
这给出了以下错误消息:
../test.cpp:103:35: error: invalid operands to binary expression ('const char *' and 'LLnode *')
report = "Front_Node[address: " + theLLHeader + " data: " + data + "]\n";
~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
我知道指针不是字符串(尽管它与 cout 在不同的函数中效果很好)。我尝试使用static_cast 无济于事,并且在搜索谷歌时看到了一些相关问题,但无法理解答案。有没有一种简单的方法来解决这个问题,还是我应该坚持单独返回数据,这已经是一个字符串?
【问题讨论】:
-
使用这个问题的
stringstream方法:stackoverflow.com/questions/7850125/… -
这可以使用字符串流来完成。在这里看到类似的问题stackoverflow.com/questions/7850125/…