LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2015, 06:11 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
how to in initialize a stringstream


Hi All,
hope someone can tell me how to initialize a stingstream. I have some code I use to send data over the network, this uses stringstream to convert numbers to a stream. It works great. However I've decided to clean my code up with Cppchecker. This is giving me the following error
Uninitialized variable: ss

my code is
Code:
template <class T> std::string to_string(const T& t)
{
    std::stringstream ss ;
    ss << t;
    return ss.str();
}
how can I set this line up std::stringstream ss = ........;

Thanks in advance!
 
Old 01-09-2015, 06:32 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,851
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
I don't know C++, but I'd try this:
Code:
template <class T> std::string to_string(const T& t)
{
    std::stringstream ss();
    ss << t;
    return ss.str();
}
 
Old 01-09-2015, 08:09 AM   #3
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
Thanks but that just gives a load of errors.
 
Old 01-09-2015, 09:10 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,851
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
Next try:
Code:
#include <string>
#include <sstream>

template <class T> std::string to_string(const T& t)
{
    std::stringstream ss = 0;
    ss << t;
    return ss.str();
}
(Of course I cannot tell what your code does, or if it does anything at all.)
 
Old 01-09-2015, 12:02 PM   #5
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,125

Rep: Reputation: 1260Reputation: 1260Reputation: 1260Reputation: 1260Reputation: 1260Reputation: 1260Reputation: 1260Reputation: 1260Reputation: 1260
You could try ostringstream to limit it to output-only.
 
Old 01-09-2015, 02:53 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,774

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
I think
Code:
std::stringstream ss(std::ios_base::out);
would work. Although really this is a flaw in CppChecker, because the variable is in fact initialized regardless.
 
Old 01-10-2015, 06:00 AM   #7
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
Thanks all. I'll give the last one a try why I'm back at my pc, I think that might work. I have noticed that cppchecker which seems very good, seems to give warnings about things that for the life of me I don't think are wrong. But still I feel it would be better if everything has passed
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
stringstream::write() crash on 64-bit platform aryan1 Programming 1 02-03-2010 03:30 PM
stringstream overwriting memory ineya Programming 5 10-14-2009 12:43 PM
stringstream problem deVas Programming 1 02-14-2007 09:05 PM
stringstream as class member variable xhi Programming 15 10-13-2006 11:52 AM
c++ stringstream greg108 Programming 7 06-24-2005 07:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration