程序灰猫 2015-09-07 04:44 采纳率: 33.3%
浏览 3771
已采纳

c++声明结构体指针变量提示没有构造函数错误

##在OJ中做题
###题目描述
输入一个链表,从尾到头打印链表每个节点的值。


我的代码:


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

class Solution {
public:
    vector<int> printListFromTailToHead(struct ListNode* head) {
        struct ListNode* p1, p2;
        p1 = head;
        p1->next = NULL;
        p2 = p1->next, p2->next = p1;
        p1 = p2;
        while(p1->next != NULL) {
            p2 = p1->next;
            p2->next = p1;
            p1 = p2;
        }
        //现在p1指向新链表头
        vector<int> out;
        int i = 0;
        while(p1 != NULL) {
            out[i++] = p1->val;
            p1 = p1->next;
        }
    }
};

我想先反转链表再顺序输出,可是结构体变量声明那一行提示没有构造函数错误(去掉结构体定义的注释号提示结构体重复定义,所以我认为结构体在后台已经定义过了)
错误信息:
编译错误:您提交的代码无法完成编译

In file included from main.cc:2:
./solution.h:13:30: error: no matching constructor for initialization of 'struct ListNode'
struct ListNode* p1, p2;
^
../../include/tools.h:115:2: note: candidate constructor not viable: requires single argument 'x', but no arguments were provided
ListNode(int x) :
^
../../include/tools.h:112:8: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
struct ListNode {
^
../../include/tools.h:112:8: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
1 error generated.
提交运行

  • 写回答

2条回答 默认 最新

  • bdmh 移动开发领域优质创作者 2015-09-07 04:51
    关注

    加一个无惨构造函数

     ListNode(){}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?