简体   繁体   English

使用链接列表和模板的我的ctor有什么问题?

[英]What is wrong with my ctor using linked list and templates?

I am trying to practice a little bit with cpp and want to make a class of linked list and than to manipulate it reverse it, finding circle and etc. but I can't understand what is the problem with my ctor/ 我正在尝试与cpp一起练习一点,并想制作一类链表,而不是对其进行反向操作,查找圆形等。但是我不明白我的ctor /是什么问题

I have this main: 我有这个主要的:

#include "list.h"
#include <iostream>


using namespace std;


int main () {
  int x = 10;

  ListNode<int> node4();
  ListNode<int> node3(10 ,node4);
}

while this is "list.h" : 这是“ list.h”时:

#ifndef LIST_NODE_H
#define LIST_NODE_H

#include <iostream>
using namespace std;

template <class T>
class ListNode {

  ListNode* next;
  T data;

 public:

  ListNode<T>( T dat = 0 ,const ListNode<T> &nex = NULL):data(dat),next(nex){};

};

#endif

I can't understand why this line: ListNode<int> node3(10 ,node4); 我不明白为什么这行: ListNode<int> node3(10 ,node4); makes this errors, what is the problem? 出现此错误,是什么问题?

list.cpp: In function 'int main()': list.cpp:12:33: error: invalid conversion from 'ListNode ( )()' to 'int' [-fpermissive] list.h:15:3: error: initializing argument 1 of 'ListNode::ListNode(T, const ListNode&) [with T = int]' [-fpermissive] list.cpp:12:33: warning: passing NULL to non-pointer argument 1 of 'ListNode::ListNode(T, const ListNode&) [with T = int]' [-Wconversion-null] list.cpp:12:33: error: recursive evaluation of default argument for 'ListNode::ListNode(T, const ListNode&) [with T = int]' list.h:15:3: warning: in passing argument 2 of 'ListNode::ListNode(T, const ListNode&) [with T = int]' [enabled by default] list.h: In constructor 'ListNode::ListNode(T, const ListNode&) [with T = int]': list.cpp:12:33: instantiated from here list.h:15:76: error: cannot convert 'const ListNode' to 'ListNode ' in initialization list.cpp:在函数'int main()'中:list.cpp:12:33:错误:从'ListNode( )()'到'int'[-fpermissive] list.h:15:3:错误的转换无效:初始化'ListNode :: ListNode(T,const ListNode&)[[T = int]“的参数1 [-fpermissive] list.cpp:12:33:警告:将NULL传递给'ListNode ::的非指针参数1 ListNode(T,const ListNode&)[with T = int]'[-Wconversion-null] list.cpp:12:33:错误:递归评估'ListNode :: ListNode(T,const ListNode&)[]的默认参数= int]'list.h:15:3:警告:传递'ListNode :: ListNode(T,const ListNode&)的参数2 [T = int]'[默认启用] list.h:在构造函数'ListNode中:: ListNode(T,const ListNode&)[with T = int]':list.cpp:12:33:从此处实例化list.h:15:76:错误:初始化时无法将'const ListNode'转换为'ListNode '

You have a number of errors with your code: 您的代码有很多错误:

  1. In your constructor you have a null reference as your default value, which is invalid (see here ). 在构造函数中,您有一个null引用作为默认值,这是无效的(请参阅此处 )。

  2. For a constructor with no arguments you need to omit the () (you can read about why here ) 对于不带参数的构造函数,您需要省略() (您可以在此处了解原因)

Something like this should work: 这样的事情应该起作用:

using namespace std;

template <class T>
class ListNode {
    ListNode* next;
    T data;

    public:
          ListNode<T>( T dat = 0 ,ListNode<T> * nex = NULL):data(dat),next(nex){};
};


int main(){
    ListNode<int> node4;
    ListNode<int> node3(10, &node4);
}

There are a couple of problems here. 这里有两个问题。

  • First you don't need ListNode<T>() simpley ListNode(...) for the constructor. 首先,构造函数不需要ListNode<T>()简单的ListNode(...)

  • Second you can't have a defalut for a reference parameter to null if there is the potential for nex to be null it needs to be a pointer. 其次,如果nex可能为null,则您不能将引用参数默认为null,而必须将其作为指针。

Default parameters are evil (IMO). 默认参数是邪恶的(IMO)。

(just a thought) Create different constructors (one with no parameters, one with parameters, ... ) (只是一个想法)创建不同的构造函数(一个不带参数的构造器,一个不带参数的构造器,...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM