Hmm, your program compiles if I add the following onto the end of srz.cpp, and remove everything from serialization.cpp:
Code:
template <class T>
T Serialization<T>::load(const string& fileName) throw(SerializationException, fileUnAccessible, fileUnReadable){
ifstream srzFStream(fileName.c_str(), ios_base::binary);
if(srzFStream.fail()){
//throw fileUnAccessible(typeid(T).name(), fileName);
}
T obj;
srzFStream.read((char*)&obj, sizeof(obj));
return obj;
}
template <class T>
void Serialization<T>::store(T obj, const string& fileName) throw(SerializationException, fileUnAccessible, failedToWrite){
ofstream srzFStream(fileName.c_str(), ios_base::binary);
if(srzFStream.fail()){
//throw fileUnAccessible(typeid(obj).name(), sizeof(obj), fileName);
return;
}
srzFStream.write((char*)&obj, sizeof(obj));
if(srzFStream.fail()){
//throw failedToWrite(typeid(obj).name(), sizeof(obj), fileName);
return;
}
}
The strange thing is that the commented-out throws give errors when this code is in srz.cpp. (failedToWrite() has only a default constructor, so of course it gives an error.) Yet, in serialization.cpp, the same code gives no errors at all.
The same thing happens if I compile the code by hand.
So it seems like serialization.cpp is jinxed or something. I created new.cpp, a new source file to hold the bodies of those two static functions. I got the same errors.
Moving main() to new.cpp, and leaving the function bodies in srz.cpp, creates the same errors. Grr. So main() has to be in the same file as those functions?
By the way, I finally got your code to compile by using this for serialization.h:
Code:
/***************************************************************************
* Copyright (C) 2008 by Neel Basu *
* neel@zigmoyd *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef SERIALIZATION_H
#define SERIALIZATION_H
#include <fstream>
#include <iostream>
#include <string>
#include <typeinfo>
#include "serializationexception.h"
using std::string;
using std::ofstream;
using std::ifstream;
using std::ios;
using std::ios_base;
template <class T>
class Serialization{
public:
//static T load(const string&) throw(SerializationException, fileUnAccessible, fileUnReadable);
//static void store(T, const string&) throw(SerializationException, fileUnAccessible, failedToWrite);
static T load(const string& fileName) throw(SerializationException, fileUnAccessible, fileUnReadable){
ifstream srzFStream(fileName.c_str(), ios_base::binary);
if(srzFStream.fail()){
//throw fileUnAccessible(typeid(T).name(), fileName);
}
T obj;
srzFStream.read((char*)&obj, sizeof(obj));
return obj;
}
static void store(T obj, const string& fileName) throw(SerializationException, fileUnAccessible, failedToWrite){
ofstream srzFStream(fileName.c_str(), ios_base::binary);
if(srzFStream.fail()){
//throw fileUnAccessible(typeid(obj).name(), sizeof(obj), fileName);
return;
}
srzFStream.write((char*)&obj, sizeof(obj));
if(srzFStream.fail()){
//throw failedToWrite(typeid(obj).name(), sizeof(obj), fileName);
return;
}
}
};
#endif
So I guess your two functions have to be inline, in the header file, or visible from where they are declared. Someone more versed in C++ probably could have told you that immediately and why . . . .