combine "FileHandler Class Header" and "File Handler Class Implementation" in one file.
C++ Class Template Issue [duplicate]
-
16-07-2023 - |
Question
I get an error in my program saying "Unresolved External symbol", i tried everything i know but couldn't solve it. I started getting this error after i used class template.
Here is FileHandler Class Header :-
#include <string>
#include <iostream>
#include "test.h"
#include <iomanip>
#include "Administrator.h"
#ifndef FileHandler_h
#define FileHandler_h
using namespace std;
template<class T>
class FileHandler
{
public:string writeToFile(T writeObject);
public: FileHandler();
};
#endif
File Handler Class Implementation
template<class T>
FileHandler<T>::FileHandler(){}
template<class T>
string FileHandler<T>:: writeToFile(T writeObject)
{
ofstream outputFile;
outputFile.open("file.txt",ios::out|ios::app);
more code..........
return "done";
}
Here is the code where i call the method in filehandler class
string Administrator::registerNewMember(Administrator newAdmin)
{
FileHandler<Administrator> f1;
return f1.writeToFile(newAdmin);
}
Below is a screen shot of the error which i get.
How do i fix this error?
Solution
OTHER TIPS
Put the definitions from .cpp
beside the declarations in the header file .h
. Or you need explicit instantiasion.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow