错误:无法将参数 1 的 Elem 转换为 Elem* 到 void addHead(Elem*, Elem*)

发布于 2024-12-09 11:57:11 字数 787 浏览 1 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(1

药祭#氼 2024-12-16 11:57:11

它非常具体:您正在传递一个 Elem,但它需要一个 Elem*。特别是, head 的类型为 Elem*,但您传递的 *head: 类型为 Elem

此外,您的定义签名与您的定义不匹配,因此即使您修复了调用,当找不到 addHead(Elem*, Elem*) 时,您也会收到链接器错误。定义必须完全它们的签名(更准确地说是声明)。当然,这些更改都不会修复 addHead() 的实际实现,但这是您的作业:)

It's being pretty specific: You're passing an Elem, but it takes an Elem*. In particular, head is of type Elem*, but you are passing *head: of type Elem.

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 of addHead(), but that's your homework :)

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