Error:no matching function for call to......&leetcode2
程序员文章站
2022-03-30 21:56:03
...
刷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;
}
};
推荐阅读
-
PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解决办法
-
Fatal error: Call to undefined function curl_init()解决方法
-
jQuery的$.get()函数不执行以及php端报错Uncaught Error: Call to a member function bind_param() on boolean in...
-
Fatal error: Call to undefined function json_decode()解决办法
-
Uncaught Error: Call to undefined function imageCreateTruecolor() in ,php7中GD库打不开
-
PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解决办法
-
【】使用cURL库时出现Fatal error: Call to undefined function curl_init()
-
php用mysqli模块连数据库Fatal error: Call to a member function query() 。解决思路
-
php连接mysql出错:Fatal error: Call to undefined function mysql_connect(),该如何处理
-
求大神纠错,多谢!Fatal error: Call to a member function query() on a non-object