This question has already been answered:
- To render the template class method in .cpp 3 responses
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.