Public Group

# [C++] stringstream

This topic is 3486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to use stringstream, but it always gives me an error, and I don't know why.. I included iostream, string and sstream just to be sure it's not the problem..
#include <iostream>
#include <string>
#include <sstream>

...
stringstream stm("String");
...


What am I doing wrong? Thanks for any helpers, Portishead.

##### Share on other sites
std::stringstream , unless you put the line

using namespace std;

##### Share on other sites
Some more source code, and perhaps what error you are seeing would certainly help. As godsenddeath said, you are likely not dealing with the std namespace correctly.

##### Share on other sites
Didn't know it was in std namespace..

Now I'm trying to do this, but it still gives an error:
std::stringstream stm;//Also tried this:std::stringstream stm("string");

Btw, I'm in a windows application, and these are the headers I include:
iostream,
window,
commctrl.

##### Share on other sites
Try using std::ostringstream (for when you want to write to the string) or std::istringstream (for when you want to read from it) instead. And you will need to include sstream.

##### Share on other sites
Quote:
 Original post by PortisheadNow I'm trying to do this, but it still gives an error:*** Source Snippet Removed ***

You do realize there are thousands if not millions of errors one can get from faulty code? We're not psychic; we need the actual error.

##### Share on other sites
This is the error:
error C2079: 'name_stm' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>'

And this is what I'm trying to do:
I have a counter (int), and a string(char*, not std::string), and I'm trying to add the counter to the string each time I call the function(Name1, Name2...).

##### Share on other sites
I would recommend using std::string and boost::lexical_cast. char* is error prone and doesn't have the convenient functionality of std::string. To quote Zahlman - "As a general rule, if you post in For Beginners and your code contains the word 'char', you have a bug. std::string roxors teh big one one one one."

Anyway, having the actual error helps, but it's kind of ambiguous as to exactly what is wrong. Can you post the code?

[Edited by - MikeTacular on April 29, 2009 1:05:28 PM]

##### Share on other sites
Quote:
 Btw, I'm in a windows application, and these are the headers I include:iostream,window,commctrl.

Your earlier code showed you including something else (like <sstream>). Don't post fragments that are meaningless on their own. Post a real thing like:

#include <iostream>#include <string>using namespace std;int main(){    stringstream ss("hello world"); //doesn't compile}

##### Share on other sites
this compiles

#include <sstream>#include <string>#include <iostream>int main(){	std::stringstream ss("str");	return 0;}*EDIT i should mention that i only threw <string> and <iostream> in because you had it, you don't have to include those headers to use stringstream

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633300
• Total Posts
3011266
• ### Who's Online (See full list)

There are no registered users currently online

×