C++ 返回字符串函数指针中的地址
我正在编写一组具有特定需求的链表函数。我必须编写一个具有以下要求的函数C++ 返回字符串函数指针中的地址,c++,pointers,C++,Pointers,我正在编写一组具有特定需求的链表函数。我必须编写一个具有以下要求的函数 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
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\u cast
,但没有效果,在搜索谷歌时看到了一些相关问题,但无法理解答案。有没有一个简单的方法来解决这个问题,或者我应该只返回已经是字符串的数据吗?这里是函数的修改版本
字符串检索前(LLnode*theLLHeader)
{
字符串数据、报表;
if(theLLHeader==nullptr)
{
抛出“此列表为空”;
}
其他的
{
数据=标题->数据;
细流ss;
ss您可以使用stringstream来实现这一点
std::stringstream ss;
ss << "Front_Node[address: " << static_cast<const void*>(theLLHeader) << " data: " << data << "]\n";
report = ss.str();
return report;
std::stringstream-ss;
学生使用这个问题中的stringstream
方法:这可以通过使用stringstream来完成。参见这里的类似问题
std::stringstream ss;
ss << "Front_Node[address: " << static_cast<const void*>(theLLHeader) << " data: " << data << "]\n";
report = ss.str();
return report;