Jump to content

Serializer problem writing template to fstream


ParaToxic
 Share

Recommended Posts

Hello guys,

I had a little idea for a data loader and saver.Normal functions uses void pointers or just void to write/read data from a binary fstream object.

But why don't you use templates for that ??

 

So I used them, but it doesn't work actually ( I'm not the pro with templates...so...)

 

Maybe someone can help me.. I get a linker error when I want to call the write function.

 

#ifndef ISERIALIZER_H_
#define ISERIALIZER_H_
#include <iostream>
#include <fstream>
#include "GlobalDefine.h"
class ISerializer
{
public:
ISerializer() : fileloaded(false) { }
~ISerializer() { }
std::fstream filestream;
bool fileloaded;
IResult Load(std::string filename);
IResult Save();
template<typename T>
void Read(T* out);
template<typename T>
void Write(T in);

};

#endif

 

#include "ISerializer.h"

IResult ISerializer::Load(std::string filename)
{
filestream.open(filename.c_str(),std::ios::in | std::ios::binary);
if(!filestream.bad())
{
 return IRESULT_OK;
} else {
 return IRESULT_ERROR;
}
}
IResult ISerializer::Save()
{
filestream.close();
fileloaded = false;
return IRESULT_OK;
}
template<typename T>
void ISerializer::Read(T* out)
{
if(fileloaded)
{
 filestream.read(&out,sizeof(T));
}
}
template<typename T>
void ISerializer::Write(T in)
{
if(fileloaded)
{
 filestream.write(in,sizeof(T));
}
}

 

Thanks ;)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...