I created two classes Student and List. And as soon as I try to add student obj to a list it calls destructor. I somewhere read that it might be because it can't allocate memory, but maybe it is not the issue this time. I will post only relavent code.
The unnamed temporary object (argument to studentList.add in line 3 of main) is destroyed when the function returns. This happens after that temporary object has been copied (copy-assigned) in List<>::_addElementAtEnd(), so this is as it should be.
List is a template; move the contents of list.cpp into the header (and throw list.cpp away).
See: 'Why can’t I separate the definition of my templates class from its declaration and put it inside a .cpp file?' http://isocpp.org/wiki/faq/templates
I have third header file with contains list.h and list.cpp. When I do that everthing works ok. Is it okey to do this way? Then I can keep definition separate from implementation.
And the issue is not about temp object Student(), but when main function ends(at return statement) program crashes with BLOCK_TYPE_IS_VALID(pHead->nBlockUse) line 52. I googled it and it occures when something is tried to delete multiple times.
> I have third header file with contains list.h and list.cpp. When I do that everthing works ok. Is it okey to do this way?
Yes. Ideally do not call the second file list.cpp; instead use a name like list.inl
> when main function ends(at return statement) program crashes
You need to write a proper copy constructor and an overloaded assignment operator for the class List<> (deep copy is required).
(Class Student has the member _grades which is a list.)