Operator "+" unusual overload
Apr 26, 2015 at 3:34pm UTC
Hey. Im working on a program and i could use a help overloading operator "+".
I want it to merge 2 files together, something like this :
I tried it like this but I am getting bunch of errors :
1 2 3 4 5 6 7 8 9 10
template <typename T>istream& operator + <T>(istream &in,istream&in2)
{
ofstream merged(in+"_MERGE.ini" );
merged<<in.rdbuf()<<endl<<in2.rdbuf();
return merged;
}
And the implementation is then :
1 2 3 4 5 6 7 8 9
string FILE1NAME,FILE2NAME,FILE3NAME;
FILE1NAME="First" ;
FILE2NAME="Second" ;
FILE3NAME="TEST" ;
ifstream file2(FILE1NAME+".ini" );
ifstream file3(FILE2NAME+".ini" );
ofstream fileTest(FILE3NAME+".ini" );
fileTest=file2+file3;
Thanks for any help.
Last edited on Apr 26, 2015 at 3:37pm UTC
Apr 26, 2015 at 3:51pm UTC
Your attempt claims to return istream&, but actually returns an ofstream automatic variable. Wrong type and a value that will cease to exist.
I would not write such overload. You seem to desire concatenation. Therefore:
ifstream in1;
ifstream in2;
ofstream out1;
copy all from in1 to out1
copy all from in2 to out1
Apr 26, 2015 at 3:53pm UTC
Thank you very much for your answer. I am trying to do what you just wrote, but I'm trying to do it using the overloaded operator "+". Do you think that would be possible ?
Topic archived. No new replies allowed.