Error Putting String into Stream

For this project I need to parse a string using spaces as a delimiter. Since stringstreams do this by default I figured it was a good idea to use one.

My code looks like this:
1
2
3
4
5
6
7
8
9
#include <sstream>

myfunction(string expression){
        stringstream ss;
	string temp;

	ss(expression);
	while(ss >> temp){//code to put the parsed string into a stack}
}


I keep getting the following error when I try to compile however:
"error: no match for call to ‘(std::stringstream {aka std::__cxx11::basic_stringstream<char>}) (std::__cxx11::string&)’
ss(expression);"

From C++ documentation I thought that string streams accepted a string in the initializing function. Any ideas what I am doing wrong?
Your syntax is wrong. Also, here are a couple of improvement-helps:

1
2
3
4
5
6
7
8
9
foo myfunction( const string& expression )
{
        string temp;
        istringstream ss( expression );
        while (ss >> temp)
        {
                baz.push_back( temp );  // or whatever you do to add to the stack
        }
}

Hope this helps.
This worked for me! But where exactly did I mess up in the syntax? Is it incorrect to implement
stringstream ss(string); as,

1
2
 stringstream ss;
ss(string);

, or was the issue that I was using "stringstream" instead of "istringstream"? Or both issues possibly?

Also, good call on the const. I didn't think about that.
Line 2 is not calling a constructor. It is attempting to call a function operator in the stringstream class, but there isn't one defined, so you get a compile error.

You could set the string explicitly with ss.str( string );. That is not as useful as you'd think, though. Better to declare and initialize at the same time.

Hope this helps.
Topic archived. No new replies allowed.