-
January 25th, 2011, 10:59 AM
#1
[beginner]streams, operator >> overloading troubles
Ok, I'll first layout code and then ask question:
String.h
Code:
#ifndef String_HEADER_INCLUDED
#define String_HEADER_INCLUDED
#include <iostream>
#include <sstream>
namespace testNamespace
{
class String
{
public:
std::stringstream sstr;
friend std::ostream& operator << (std::ostream& os,const String& obj)
{
os << obj.sstr.str();
return os;
}
friend std::istream& operator >> (std::istream& is, const String& obj)
{
is >> obj.sstr.str();// HERE LIES MY PROBLEM
return is;
}
};
};
Main.cpp
Code:
#include <fstream>
#include "String.h"
int main()
{
testNamespace::String outStr;
outStr.sstr << "This is some test text to use!" << std::endl;
std::string fileName = "test.txt";
//HERE IS COMMENTED PART WHERE
// i WAS PREVIOUSLLY WRITTEN TEXT TO A FILE
//std::fstream outStream(fileName, std::ios::out);
//outStream << outStr << std::endl;
//outStream.close();
testNamespace::String inStr;
std::ifstream inStream(fileName, std::ios::in);
inStream >> inStr;
inStream.close();
std::cout << "TEXT FROM FILE :\t" << inStr << std::endl;
std::cin.get();
return 0;
}
Its simple console project to learn some STL functionality and operator overloading.
I have outputted file with proper name and content but can't get content
back in from file into my string object, nothing is displayed in console.
Can you look in "String.h" and tell me how should i properly add this operator to work?
Where i am making mistake?
If you need additional info just tell me.
-
January 25th, 2011, 11:20 AM
#2
Re: [beginner]streams, operator >> overloading troubles
Code:
is >> obj.sstr.str();// HERE LIES MY PROBLEM
I think that stringstream::str() returns a copy of the internal
string, not a reference.
I would suggest using a std::string as the member variable instead
of a stringstream.
I think that strinstream objects are not copyable, so you could
not make copies of String objects if you keep a stringstream
as a member variable.
-
January 25th, 2011, 11:52 AM
#3
Re: [beginner]streams, operator >> overloading troubles
I was using std::string before, but it doesnt have operator = + += etc. for primitive number types so i added it in my custom String class by using:
Code:
...
String& String::operator = (int inum)
{
char buffer[MAX_CHARS_IN_INUM];
errno_t err = _itoa_s(inum, buffer, MAX_CHARS_IN_INUM, 10);
if(0 == err)
{
str = buffer;//where str is object of std::string type
}
return *this;
}
But later, when i heard about std::stringstream that it has that ability:
Code:
int someNumber = 50;
std::stringstream str;
str << "Number is " << someNumber;
i decided to use this instead of std::string. I was hoping for some simple solution to this by not adding some buffer to pass to string, its redundant.
-
January 25th, 2011, 12:38 PM
#4
Re: [beginner]streams, operator >> overloading troubles
Originally Posted by borko1980
I was using std::string before, but it doesnt have operator = + += etc. for primitive number types so i added it in my custom String class by using:
Code:
...
String& String::operator = (int inum)
{
char buffer[MAX_CHARS_IN_INUM];
errno_t err = _itoa_s(inum, buffer, MAX_CHARS_IN_INUM, 10);
if(0 == err)
{
str = buffer;//where str is object of std::string type
}
return *this;
}
But later, when i heard about std::stringstream that it has that ability:
Code:
int someNumber = 50;
std::stringstream str;
str << "Number is " << someNumber;
i decided to use this instead of std::string. I was hoping for some simple solution to this by not adding some buffer to pass to string, its redundant.
But as stated, streams are not copyable. Making a stream a member of a class produces undefined behaviour (if the code does compile) if a copy is made.
Second, use std::ostringstream, not stringstream.
Third, why not just write a toString() template function?
Code:
#include <sstream>
#include <string>
#include <iostream>
template <typename T>
std::string toString(T val)
{
std::ostringstream strm;
strm << val;
return strm.str();
}
int main()
{
std::string s = toString(3) + " " + toString(4.3);
std::cout << s;
}
Regards,
Paul McKenzie
-
January 25th, 2011, 12:46 PM
#5
Re: [beginner]streams, operator >> overloading troubles
Thanks. But i was hoping to not include some temporary variables, but to write directly to it.
-
January 25th, 2011, 02:16 PM
#6
Re: [beginner]streams, operator >> overloading troubles
You could use boost::lexical_cast, but this is more or less equivalent to the template function Paul mentions.
Don't get too worked up about the excess string copying-----much of it will go away when you compile optimized anyway.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|