c++ 中的类型转换和引用

发布于 2024-12-21 01:33:02 字数 750 浏览 1 评论 0原文

请看下面的调用和相应的函数,

long pagenumber = 0;
Node *newNode = createNode();
bufMgr->writePage(pageNumber,(char*)newNode);

writePage 声明如下

writePage(long &pageNumber,char* &node)

writePage 接受长引用和 char* 引用。 上面的内容无法编译,它显示以下错误

no matching function for call to ‘SampleBufferManager::writePage(long int&, char*)’
SampleBufferManager.h:28: note: candidates are: bool SampleBufferManager::writePage(long int&, char*&)

任何人都可以帮助我,如何处理这个问题...类型转换正在解决问题,如果我按照以下方式执行代码,代码就会编译:

long pagenumber = 0;
Node *newNode = createNode();
char *test = (char*)newNode;
bufMgr->writePage(pageNumber,test);

问题怎么可能解决了??

Please look at the following call and the corresponding function,

long pagenumber = 0;
Node *newNode = createNode();
bufMgr->writePage(pageNumber,(char*)newNode);

and writePage is declared as follows

writePage(long &pageNumber,char* &node)

writePage accepts a long reference and char* reference.
The above doesn't compile, It shows the following error

no matching function for call to ‘SampleBufferManager::writePage(long int&, char*)’
SampleBufferManager.h:28: note: candidates are: bool SampleBufferManager::writePage(long int&, char*&)

Can anyone help me out, how to handle this...The typecast is doing the problem, and the code compiles if i do it in the following way:

long pagenumber = 0;
Node *newNode = createNode();
char *test = (char*)newNode;
bufMgr->writePage(pageNumber,test);

How could the problem be solved??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半仙 2024-12-28 01:33:02

您无法传递对 temporary 右值的引用(感谢好奇的家伙)。您必须创建一个变量并传递它:

char* c = (char*)newNode;
bufMgr->writePage(pageNumber, c);

You can't pass a reference to a temporary rvalue (thanks curiousguy). You'll have to make a variable and pass that:

char* c = (char*)newNode;
bufMgr->writePage(pageNumber, c);
场罚期间 2024-12-28 01:33:02

您不能将非const 引用绑定到临时引用。 (char *)newNode 的结果是临时的(它没有名称)。

但是,您可以将 const 引用绑定到临时引用。因此,将您的函数重新声明为 writePage(long &pageNumber, char* const &node) 就可以了。

You cannot bind a non-const reference to a temporary. The result of (char *)newNode is a temporary (it doesn't have a name).

You can, however, bind a const reference to a temporary. So redeclaring your function as writePage(long &pageNumber, char* const &node) would work.

油焖大侠 2024-12-28 01:33:02

writePage 是做什么的?您也控制该功能吗?如果第二个参数不写入此地址,则将第二个参数重新声明为 const 可能会有所帮助。如果确实如此(它的名字表明了这一点),无论如何在其中使用 Node* 可能是合理的,即将其声明为

writePage(long &pageNumber, Node* &node)

毕竟,您不想要您的 Node > 指针 newNode 指向根本不是 Node 实例的东西,因为它被任意 char 覆盖。你?

What does writePage do? Are you in control of that function as well? Redeclaring the second parameter as const might help, if it does not write to this address. If it does—and its name suggests that somehow—it might be reasonable to use Node*in it anyway, i.e. declare it as

writePage(long &pageNumber, Node* &node)

After all, you don't want your Node pointer newNode to point to something which is not a Node instance at all, anymore, because it was overwritten by arbitrary chars. Do you?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文