简体   繁体   English

链表c ++的节点构造器中的错误

[英]error in node constructor for Linked-list c++

i'm trying to make a LinkedList of ints and for that i created a Node class. 我试图使一个整数的LinkedList和为此,我创建了一个Node类。 but the compiler constantly gives me the same error. 但是编译器不断给我同样的错误。 i searched all over the web and the answers i found didn't work, or i did not understand them. 我在网上搜索了所有内容,发现的答案不起作用,或者我听不懂。

the error is the following: 错误如下:
line 51: new types may not be defined in a return type 第51行:返回类型中可能未定义新类型
line 51: return type specification for constructor invalid 第51行:构造函数的返回类型说明无效

all 4 constructors have the same problem, the error in line 51 is just the first one of the four. 所有4个构造函数都有相同的问题,第51行中的错误只是这四个中的第一个。

here is my Node class with all it has. 这是我所有的Node类。 i didn't copy the getters and setters because they have no errors, and the code is semi-obvious. 我没有复制getter和setter,因为它们没有错误,并且代码是半明显的。

thanks a lot. 非常感谢。 Andy 安迪

class Node{

private:
    int val;
    Node* pPrv;
    Node* pNxt;

public:
    Node();
    Node(Node&);
    Node(int);    
    Node(int, Node*, Node*);

    void setVal(int auxVal);
    void setPrv(Node* auxPrv);
    void setNxt(Node* auxNxt);

    int getVal();
    Node* getPrv();
    Node* getNxt();
}

Node::Node(){ //this is line 51
    val = 0;
    pPrv = NULL;
    pNxt = NULL;
}

Node::Node(Node &node2){ //this line has exactly the same error
    val = node2.getVal();
    pPrv = node2.getPrv();
    pNxt = node2.getNxt();
}

Node::Node(int valAux){ //so does this one
    val = valAux;
    pPrv = NULL;
    pNxt = NULL;
}

Node::Node(int valAux, Node* prvAux, Node* nxtAux){ //and also this one
    val = valAux;
    pPrv = prvAux;
    pNxt = nxtAux;
}

You forgot tp place a semicolon after the class definition. 您忘记了在类定义后放置分号。

In this case the compiler considers the declaration of the class above constructor Node() 在这种情况下,编译器将考虑构造函数Node()之上的类的声明。

class Node{
//...
}
Node::Node(){
    val = 0;
    pPrv = NULL;
    pNxt = NULL;
}

as its return type. 作为其返回类型。

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

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