how can i saving a string on a file?

i overloading the operator<< and operator>> for string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::ostream& operator << (std::ostream& lhs, const string& rhs)
{
    int stringsize=rhs.size();
    lhs<<stringsize;
    lhs.write(reinterpret_cast< const char*>(&rhs),rhs.size());
    return lhs;
}

std::istream& operator >> (std::istream& lhs, string& rhs)
{
    string chrfilename;
    int stringsize;
    lhs>>stringsize;
    lhs.read(reinterpret_cast<char*>(&chrfilename), stringsize);
    rhs=chrfilename;
    return lhs;
}

when i do a structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
struct user
{
    string name;
    int age;
};

//save a structure object to a file
template<typename structure>
void SaveDataBase(string filename,structure &StructureVariable )
{
    remove(filename.c_str());
    ofstream writefile(filename.c_str(),std::ios::binary);
    writefile << StructureVariable;//usrName is a user object
    writefile.close();
}

//read a structure object from a file
template<typename structure>
void ReadDataBase(string filename,structure &StructureVariable )
{
    ifstream readfile(filename.c_str(),std::ios::binary);
    readfile >> StructureVariable;
    readfile.close();
}
//using it:
user usrName;
user usrName2;

usrName.age=23;
usrName.name="joaquim miguel";
SaveDataBase("username1.dat",usrName);
ReadDataBase("username1.dat",usrName2);
mnuExitSys2.Caption=usrName2.name;//getting the name 

on SaveDataBase():
writefile << StructureVariable;
i get an error:
"cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'"
what anyone can tell me?
there are already overloaded operators for string file output and input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <fstream>

std::ifstream in("in.txt");
std::ofstream out("out.txt");

int main(){

	std::string w;
	in >> w;
	out << w;

	return 0;
}
yes you have right. i was doing some errors and the problem is on my image overloaded operators for file output and input.
thanks for all
Topic archived. No new replies allowed.