Help with C++ Template/FileIO/Operator overloading please

CorneN

Expert Member
Joined
Sep 20, 2008
Messages
1,406
Consider the following:

Code:
	void ReadFile(std::vector<T>& vec) {
		std::string data;
		std::fstream file;
		file.open(m_sfileName, std::ios::in);
		if(!file.fail()) {
			while(std::getline(file, data)) {
				T obj;
				std::stringstream converter(data);
				converter >> obj;
				if(!converter.fail()) {
					vec.push_back(obj);
				}
			}
			file.close();
		}
	}

The function above is part of a Template FileIO class that stores custom objects with overloaded '<<' and '>>' operators. Works perfect, all fine...

BUT, if you pass it a vector<std::string> it only reads the first word of the string (surprise surprise) because converter only reads until the first space, unless the object has an overloaded '>>' operator.

So, my question is: Is it legal / advisable to create an overloaded '>>' operator for std::string?

Sorry if the question seems daft. C+ is hobby, not a job. So the exposure is not that great. :)
 

CorneN

Expert Member
Joined
Sep 20, 2008
Messages
1,406
Ok, dont stress, I got it. Will have to create a small custom object to hold the string val and overload it's ops.

tks
 

sn3rd

Expert Member
Joined
Jan 18, 2008
Messages
4,305
Sorry dude, most of us are only really around during the day.
 
Top