c++ 中的类型转换和引用
请看下面的调用和相应的函数,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法传递对
temporary右值的引用(感谢好奇的家伙)。您必须创建一个变量并传递它:You can't pass a reference to a
temporaryrvalue (thanks curiousguy). You'll have to make a variable and pass that:您不能将非
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 aswritePage(long &pageNumber, char* const &node)
would work.writePage
是做什么的?您也控制该功能吗?如果第二个参数不写入此地址,则将第二个参数重新声明为 const 可能会有所帮助。如果确实如此(它的名字表明了这一点),无论如何在其中使用Node*
可能是合理的,即将其声明为毕竟,您不想要您的
Node
> 指针newNode
指向根本不是Node
实例的东西,因为它被任意char
覆盖。你?What does
writePage
do? Are you in control of that function as well? Redeclaring the second parameter asconst
might help, if it does not write to this address. If it does—and its name suggests that somehow—it might be reasonable to useNode*
in it anyway, i.e. declare it asAfter all, you don't want your
Node
pointernewNode
to point to something which is not aNode
instance at all, anymore, because it was overwritten by arbitrarychar
s. Do you?