I'm using the following code to load objects (A, B and C which are subclasses of Object) from file. The compilation issue I have is from loadObjFromLine
load.h:611:33: erreur: there are no arguments to ‘loadObjFromLine’ that depend on a template parameter, so a declaration of ‘loadObjFromLine’ must be available [-fpermissive]
load.h:611:33: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
And when I use pObj = loadObjFromLine<T>(line);
, I get
load.h: In function ‘bool loadObjects(const string&, Object<T>*&)’:
load.h:611:13: erreur: ‘loadObjFromLine’ was not declared in this scope
load.h:611:30: erreur: expected primary-expression before ‘>’ token
EDIT
I know I could use loadObjFromLine<double>
but I'd like the type T to be the same used in loadObject(Object*&). Maybe it's not the right approach..
Any idea ?
template<typename T>
Object<T>* loadObjFromLine(const std::string& line)
{
std::stringstream lineStream(line);
int objType(-1);
lineStream >> objType;
Object<T> *pObj = NULL;
if (objType == 0)
{
A<T> *pO = new A<T>;
if (lineStream >> *pO)
pObj = pO;
}
else if (objType == 1)
{
B<T> *pO = new B<T>;
if (lineStream >> *pO)
pObj = pO;
}
else if (objType == 2)
{
C<T> *pO = new C<T>;
if (lineStream >> *pO)
pObj = pO;
}
return pObj;
}
template <typename T>
bool loadObjects( const std::string &filename, Object<T>*& pObj)
{
std::ifstream fileStream(filename.c_str());
if (fileStream.good())
{
std::string line;
int objType;
// Loading boundary.
while(std::getline(fileStream, line))
{
pObj = loadObjFromLine(line);
// pObj = loadObjFromLine<T>(line);
if (!pObj)
return false;
}
fileStream.close();
return true;
}
return false;
}