-3

I am trying to write a code to find the beginning of a loop in a linked list. Here is my code:

#include <iostream>

template <class T>
class LinkedList
{
public:
    struct node
    {
        T data;
        node* next;
    };
    node* head;
    LinkedList() : head(NULL){};
    void AppendToTail(T data);
    void InsertToHead(T data);
    void RemoveDuplicates();
    void PrintList();
    T lastNthNode(int N);
    bool operator==(const LinkedList<T>& L);
    node* FindBeginningNodeLoop();
};

template <class T>
node* LinkedList<T>::FindBeginningNodeLoop()
{
    if (head == NULL)
        return NULL;
    node* slow = head;
    node* fast = head;
    while (fast->next != NULL)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast)
            break;
    }
    if (fast == NULL)
        return NULL;

    slow = head;
    while (slow != fast)
    {
        slow = slow->next;
        fast = fast->next;
    }
    return slow;
}

The compiler gives the following errors:

1> main.cpp 1>c:\users...\linkedlist.h(177): error C2143: syntax error : missing ';' before '*' 1>c:\users...\linkedlist.h(177): error C2065: 'T' : undeclared identifier 1>c:\users...\linkedlist.h(177): error C2923: 'LinkedList' : 'T' is not a valid template type argument for parameter 'T' 1>c:\users...\linkedlist.h(200): error C2509: 'FindBeginningNodeLoop' : member function not declared in 'LinkedList' 1> c:\users...\linkedlist.h(5) : see declaration of 'LinkedList'

I cannot find the problem. Any help is greatly appreciated. Thanks.

Robomatt
  • 35
  • 4
  • Here is the only error message from my compiler, verbatim. `blah.cpp:24:1: error: unknown type name 'node' node* LinkedList::FindBeginningNodeLoop()` – ftynse Feb 16 '15 at 14:16
  • There are only 47 lines in the code you posted. Where is line 177? – Frédéric Hamidi Feb 16 '15 at 14:18
  • possible duplicate of [What is an 'undeclared identifier' error and how do I fix it?](http://stackoverflow.com/questions/22197030/what-is-an-undeclared-identifier-error-and-how-do-i-fix-it) – sashoalm Feb 16 '15 at 14:38
  • The code was compiling with no errors until I added FindBeginningNodeLoop() function. I knew that the problems were related with this function only. – Robomatt Feb 16 '15 at 14:42

2 Answers2

2

node is an inner type of LinkedList. You have twooptions:

use trailing return type, where the return type is in the scope of the class:

LinkedList<T>::FindBeginningNodeLoop()->node* { .... }

Or use the class's scope when referring to it outside of the class definition:

template <class T>
typename LinkedList<T>::node* LinkedList<T>::FindBeginningNodeLoop() { .... }
         ^^^^^^^^^^^^^^^

On top of that you need to use typename to specify that node is a type (see when is the typename keyword necessary for more information.)

The second version is required if your compiler does not support C++11 or c++14.

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

You declared struct node within LinkedList class. The name node is not visible by itself, it should be preceded by LinkedList<T>:: every time it is used outside class definition.

Also, it will require using the typename keyword to make the distinction between the type and an (eventually existing) variable of the same name.

ftynse
  • 787
  • 4
  • 9
  • Thank you very much! Now the code compiled with no errors. – Robomatt Feb 16 '15 at 14:38
  • That's not why `typename` is required. There's no mention of a variable of the same name in the code in the question and there is no reason for such variable to exist. `typename` is required because `node` is a *dependent* name. – eerorika Feb 16 '15 at 14:56
  • Well, going to the details of the standard terminology is an overkill when the question is "I can't debug a simple template". – ftynse Feb 16 '15 at 14:58
  • Well, If you don't want to scare OP with big words, then maybe you could just state that `typename` is required for reasons that they don't need to concern themselves with yet. I don't see how the incorrect statement about a variable of the same name helps. – eerorika Feb 16 '15 at 15:13