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 more
         struct 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;
    }
};

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值