0

i'm trying to make a LinkedList of ints and for that i created a Node class. 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
line 51: return type specification for constructor invalid

all 4 constructors have the same problem, the error in line 51 is just the first one of the four.

here is my Node class with all it has. i didn't copy the getters and setters because they have no errors, and the code is semi-obvious.

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;
}
3
  • 1
    You are missing a semicolon ; after your class declaration. Commented Nov 13, 2014 at 20:34
  • By the way, be careful with your copy-constructor. Copying a node can lead to nasty things happening if you remove a node from the list, and you expect the pPrv and pNxt to be valid. If you remove the node pointed to by pNxt, the original will be updated, but the copy will still have its old `pNxt pointer, now pointing to a removed node. Commented Nov 13, 2014 at 20:36
  • Do you write this list only for exercise? You can use list or deque from standard library. Commented Nov 13, 2014 at 20:51

1 Answer 1

1

You forgot tp place a semicolon after the class definition.

In this case the compiler considers the declaration of the class above constructor Node()

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

as its return type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.