I have a singly linked list implementation as shown below:
Header
class SinglyLinkedList
{
struct Node
{
Node * _pNext;
int _data;
};
public:
Node * SomeFun(Node * ip1, Node * ip2);
// Some more methods here
};
Now when implementing one of the methods of this class
CPP
Node * SinglyLinkedList::SomeFun(Node * ip1, Node * ip2)
{
//Some code and return
}
The strange behavior I am not understanding is that when compiling, the compiler refuses to recognize the type "Node" in the return type unless I specify it as SinglyLinkedList::Node. But the same type for function arguments is recognized without specifying it explicitly.. Ideally I felt that in both cases there should be no need for this explicit specifying because Node is defined in the same class. Can anyone throw some light on this?
Adding to ForEveR's answer, this issue is solved in C++11 using trailing return clauses.
In the regular style, you have not yet entered class scope (it only begins after the qualified name of the method has ended).
In the trailing return style however, the return type is in the class scope (like the arguments) and therefore the qualification not necessary.
Here, you are not in class-scope. But in parameters clause, or in function you are IN class-scope, so you shouln't qualify, that
Node
is from classSinglyLinkedList
, since compiler already knows that.n3376 3.3.7/1