LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to in initialize a stringstream (https://www.linuxquestions.org/questions/programming-9/how-to-in-initialize-a-stringstream-4175530519/)

knobby67 01-09-2015 06:11 AM

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!

NevemTeve 01-09-2015 06:32 AM

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();
}


knobby67 01-09-2015 08:09 AM

Thanks but that just gives a load of errors.

NevemTeve 01-09-2015 09:10 AM

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.)

smallpond 01-09-2015 12:02 PM

You could try ostringstream to limit it to output-only.

ntubski 01-09-2015 02:53 PM

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.

knobby67 01-10-2015 06:00 AM

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


All times are GMT -5. The time now is 03:15 AM.