I'm new to C++. I am initializing a class object Node
in another class LCache
. When I try to compile my code, I get the following error:
Line 22: Char 5: error: unknown type name 'left'
left -> next = right;
^
Below is the code I have written:
class Node{
public:
int k;
Node* prev;
Node* next;
Node (int key){
k=key;
prev=NULL;
next=NULL;
}
};
class LCache {
private:
Node* left = new Node(0);
Node* right = new Node(0);
left -> next = right;
right -> prev = left;
...
When I move left -> next = right;
and right -> prev = left;
inside a method in class LCache
, the error goes away. Can you explain the reason here?