Masious
Masious

Reputation: 430

fatal error C1001: internal error trying to use templates

I'm trying to use templates for my class. When I hit the run button, I get the following error:

1>path\to\the\project\list.cpp(21): fatal error C1001: An internal error has occurred in the compiler.
1>  (compiler file 'msc1.cpp', line 1443)
1>   To work around this problem, try simplifying or changing the program near the locations listed above.
1>  Please choose the Technical Support command on the Visual C++ 
1>   Help menu, or open the Technical Support help file for more information
1>          path\to\the\project\list.cpp(48) : see reference to class template instantiation 'List<T>' being compiled

Here is my list.cpp file:

template<class T>
class List{
public :
    T * first;
    T * last;
    List<T>::List(){
        first = new T();
        first->makeRef();
        last = first;
    }

    void List<T>::add(T * a){
        last->next = a;
        last = a;
    }

    void List<T>::recPrint(){
        recPrint(1);
    }

    void List<T>::recPrint(int id){
        T * p = first;
        int i=id;
        while(!p){
            p->recPrint(i++);
            p = p->next;
        }
    }
};

Seems like I have a problem using c plus plus templates. I'm new at this and I don't know what to do.

Upvotes: 0

Views: 986

Answers (1)

Billy ONeal
Billy ONeal

Reputation: 106609

Your code is not valid. When defining member functions inline, the class name is not repeated. That is, where you have

void List<T>::add(T * a){
    last->next = a;
    last = a;
}

you want

void add(T * a){
    last->next = a;
    last = a;
}

Alternately, you can move your member function definitions out of the class definition:

template<class T>
class List{
public :
    /* ... */
    void add(T * a);
    /* ... */
};

template <typename T>
void List<T>::add(T * a){
    last->next = a;
    last = a;
}

That said, you really want to use std::vector or std::list instead of trying to make your own.

Upvotes: 1

Related Questions