Error:no matching function for call to......leetcode2

编程入门 行业动态 更新时间:2024-10-09 12:25:15

Error:no <a href=https://www.elefans.com/category/jswz/34/1763339.html style=matching function for call to......leetcode2"/>

Error:no matching function for call to......leetcode2

刷leetcode,写成如下的时候这样报错

struct ListNode *result = new ListNode();

一开始连结构体都没有初始化,然后就改成了这样。太久不用C++忘了很多。

结构体指针在使用之前必须通过初始化或赋值操作,把某个结构体变量的首地址赋给它,使它指向该结构体变量。

现在的报错内容为

error: no matching function for call to 'ListNode:ListNode()'

之后在网上查了查关于结构体的内容也没查到想要的,然后去翻了翻书,发现大部分C++的书都没有谈及结构体的内容,就去翻了C的书。

以下是结构体定义的内容。

struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};

用本地的IDE试了一下,简化成以下代码

struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};int main()
{ListNode result, *presult;presult = &result;//结果位return 0;
}

这个时候,result画红线的错误提示是‘类ListNode不存在默认构造函数 ’。

这个地方还不是很懂为什么会把ListNode判断成类,不过我觉得可能是IDE觉得他们本来就差不多也没有认真判断。

出错的原因在这:

ListNode result;

因为在结构体里面定义了构造函数就要用,所以这个result其实没什么用,把这段代码改成如下

struct ListNode* presult = new ListNode(0);

------------------------------------------------------------------------------------------------------------------------------------------

做这个题做了好久,因为之前基本上没做过题所以就当从头开始了....

数据结构学的不好所以对链表的理解有些问题,这个题更简单的方法应该是直接把结果放在l1里面,我用的方法就是新建了一个presult的链表,然后中间因为忘记了链表怎么插入出错

 struct ListNode* insert = new ListNode(add%10);struct ListNode* result = presult;while(result->next != NULL){result = result->next;}insert->next = result -> next;result->next = insert;

这是正确的操作

之前我甚至都没有用result这个指针直接往presult后面插。。

附全部代码,写的很复杂很复杂。。。。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {/*Initialize current node to dummy head of the returning list*/   struct ListNode *presult = new ListNode(0);/*Initialize carry and the result of add*/int carry = 0;int add = 0;/*loop until both ends*/while(l1 != NULL || l2 != NULL  ){/*if l1 come to end only, make it zero*/if (l1 == NULL){add = l2 -> val + carry;}else if(l2 == NULL)/*if l2 come to end only, make it zero*/{add = l1 -> val + carry;}else{/*if none of them come to an end then add them with carry*/if(l1!= NULL &&l2!= NULL){add = l1 -> val + l2 -> val + carry;}}/*add the result to the end of the list presult*/struct ListNode* insert = new ListNode(add%10);struct ListNode* result = presult;while(result->next != NULL){result = result->next;}insert->next = result -> next;result->next = insert;/*calculate the next carry*/if(add >= 10){carry = 1;}else{carry = 0;    }/*advance l1&l2*/if(l1 != NULL){l1 = l1->next;}if(l2!= NULL){l2 = l2->next;}}presult = presult->next;//if the result is morestruct ListNode* result = presult;while(result->next != NULL){result = result->next;}if(carry == 1){result ->next = new ListNode(0);result->next->val = 1;result -> next -> next = NULL;}else{result-> next = NULL;}return presult;}
};

 

更多推荐

Error:no matching function for call to......leetcode2

本文发布于:2024-02-14 11:40:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1762998.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:matching   Error   function   call

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!