Why isthis not working
main.cpp
#include "xtream.hpp"
using cpp::xtream;
int main()
{
xtream xio;
int i = 3;
xio >> i << i << (xio.to_file("xioTest.txt"), i);
while (std::cin.get() != '\n') ;
std::cin.get();
return 0;
}
xtream.hpp
#ifndef LOCK_xtream_Hpp
#define LOCK_xtream_Hpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <cassert>
namespace cpp
{
enum iostate { console, file, string };
class xtream
{
public:
xtream(iostate flag = console);
xtream(const char *str, iostate flag = string, bool binary = false);
xtream &to_file(const char *str);
template<typename T> T &operator,(T &val) { return val; }
friend xtream &operator>>(xtream &xio, int &val);
friend xtream &operator<<(xtream &xio, const int &val);
private:
std::istream *istrm;
std::ostream *ostrm;
iostate m_flag;
};
}
#endif
xtream.cpp
#include "xtream.hpp"
cpp::xtream::xtream(iostate flag)
: m_flag(flag)
{
if (m_flag == console)
{
istrm = &std::cin;
ostrm = &std::cout;
}
else if (m_flag == file)
{
istrm = new std::ifstream;
ostrm = new std::ofstream;
}
else if (m_flag == string)
{
istrm = new std::istringstream;
ostrm = new std::ostringstream;
}
}
cpp::xtream::xtream(const char *str, iostate flag, bool binary)
: m_flag(flag)
{
assert(m_flag == file || m_flag == string);
if (m_flag == file)
{
istrm = new std::ifstream(str);
ostrm = new std::ofstream(str);
}
else if (m_flag == string)
{
istrm = new std::istringstream(str);
ostrm = new std::ostringstream(str);
}
}
cpp::xtream &cpp::xtream::to_file(const char *str)
{
if (m_flag != console)
{
delete istrm;
delete ostrm;
}
istrm = new std::ifstream(str);
ostrm = new std::ofstream(str);
return *this;
}
cpp::xtream &cpp::operator>>(cpp::xtream &xio, int &val)
{
*(xio.istrm) >> val;
return xio;
}
cpp::xtream &cpp::operator<<(cpp::xtream &xio, const int &val)
{
*(xio.ostrm) << val;
return xio;
}