This question has already been answered:

The compiler does not seem to see a constructor with parameters and a destructor, but everything is there. I do not understand where this error comes from. Explain, please!

Error code:

main.o: In function `main': C:\Users\../main.cpp:14: undefined reference to `ControllerIOFStream<UserAccount>::ControllerIOFStream(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' C:\Users\../main.cpp:14: undefined reference to `ControllerIOFStream<UserAccount>::~ControllerIOFStream()' collect2.exe: error: ld returned 1 exit status 

Creating an object of class ControllerIOFStream:

MAIN.cpp

 ControllerIOFStream<UserAccount> CIOUserAccounts(str); 

The class is described in two files: ControllerIOFStream.h and ControllerIOFStream.cpp

ControllerIOFStream.h :

 /* * ControllerIOFStream.h * * Created on: 12 нояб. 2017 Π³. * Author: Π”ΠΌΠΈΡ‚Ρ€ΠΈΠΉ */ #include <string> #include <list> #include <fstream> using namespace std; template <class T> class ControllerIOFStream { private: string path; public: ControllerIOFStream(); ControllerIOFStream(string); ~ControllerIOFStream(); list<T> readFile(list<T>); list<T> writeFile(list<T>); }; 

ControllerIOFStream.cpp :

 /* * ControllerIOFStream.cpp * * Created on: 12 нояб. 2017 Π³. * Author: Π”ΠΌΠΈΡ‚Ρ€ΠΈΠΉ */ #include "ControllerIOFStream.h" template<class T> ControllerIOFStream<T>::ControllerIOFStream(): path(""){ } template<class T> ControllerIOFStream<T>::ControllerIOFStream(string path) : path(path) { } template<class T> ControllerIOFStream<T>::~ControllerIOFStream() { } template<class T> list<T> ControllerIOFStream<T>::readFile(list<T> List){ ifstream inputStream(path); size_t size; T buf; inputStream>>size; for(size_t i=0;i<size;i++){ inputStream>>buf; List.push_back(buf); } inputStream.close(); return List; } template<class T> list<T> ControllerIOFStream<T>::writeFile(list<T> List){ ofstream outputStream(path); outputStream<<List.size()<<"\n"; //for???????????????????????????????????????????????????????????? // for(list<T>::iterator it=List.begin();it<List.end();++it){ // outputStream<<*it<<"\n"; // } outputStream.close(); return List; } 

There is another question: why does the code in the writeFile (list List) method not work?

The code itself is under a bunch of question marks.

Reported as a duplicate by Abyx c ++ Nov 12 '17 at 12:51 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    Once again, the same rake ...

    You can not make the implementation of the template in a separate .cpp-file! Whence when compiling this ( ControllerIOFStream.cpp ) file to know what (with what template parameter) will be instantiated in another file?

    And in this other file ( main.cpp ) - how to know how to instantiate a template if there is no access to the implementation code?

    What you have in ControllerIOFStream.cpp should be transferred to the .h file. Or it is necessary to explicitly instantiate ControllerIOFStream<UserAccount> ...

    • That is, all template classes are implemented immediately in the header? - Herrgott
    • As a rule, yes. Or with explicit instantiation. - Harry