错误:无法将参数 1 的 Elem 转换为 Elem* 到 void addHead(Elem*, Elem*)
我正在使用 g++ -g 编译代码,并且在标题中收到错误消息。
该错误与我所做的一个函数有关,它的签名是:
void addHead( Elem *&start , Elem *newStart );
并且我将这两个变量传递给这个函数:
Elem * head;
Elem * tempEl;
所以它看起来像这样:
addHead( *head , *tempEl );
实际的函数是:
void addHead( Elem start , Elem newStart )
{
Elem listItem;
listItem = newStart;
*listItem.next = start;
start = listItem;
}
它将第二个参数预先挂在 a 的开头从第一个参数开始的链表。
我一直用这个把头发拉出来。无论我做什么,我都会收到此错误!
cannot convert Elem to Elem* for argument 1 to void addHead(Elem*, Elem*)
编辑:忘记了这个错误也在那里:
error: invalid initialization of reference of type Elem*& from expression of type Elem
I am compiling my code with g++ -g and I am getting the error message in the title.
The error is related to a function I have made, it's signature being:
void addHead( Elem *&start , Elem *newStart );
and I am passing this function these two variables:
Elem * head;
Elem * tempEl;
so that it looks like this:
addHead( *head , *tempEl );
The actual function is:
void addHead( Elem start , Elem newStart )
{
Elem listItem;
listItem = newStart;
*listItem.next = start;
start = listItem;
}
It pre-pends the second argument to the the beginning of a linked-list starting at the first argument.
I have been pulling my hair out with this one. No matter what I do I keep getting this error!
cannot convert Elem to Elem* for argument 1 to void addHead(Elem*, Elem*)
Edit: Forgot this error is in there too:
error: invalid initialization of reference of type Elem*& from expression of type Elem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它非常具体:您正在传递一个
Elem
,但它需要一个Elem*
。特别是,head
的类型为Elem*
,但您传递的*head
: 类型为Elem
。此外,您的定义签名与您的定义不匹配,因此即使您修复了调用,当找不到
addHead(Elem*, Elem*)
时,您也会收到链接器错误。定义必须完全它们的签名(更准确地说是声明)。当然,这些更改都不会修复addHead()
的实际实现,但这是您的作业:)It's being pretty specific: You're passing an
Elem
, but it takes anElem*
. In particular,head
is of typeElem*
, but you are passing*head
: of typeElem
.Also, your definition signature doesn't match your definition, so even when you fix the call, you will get a linker error when it can't find
addHead(Elem*, Elem*)
. Definitions must exactly their signatures (more correctly, declarations). Of course, neither of these changes will fix the actual implementation ofaddHead()
, but that's your homework :)