Sign in to follow this  
Portishead

[C++] stringstream

Recommended Posts

Portishead    122
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 this post


Link to post
Share on other sites
trojanman    394
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 this post


Link to post
Share on other sites
Portishead    122
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 this post


Link to post
Share on other sites
Kippesoep    892
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 this post


Link to post
Share on other sites
Cornstalks    7030
Quote:
Original post by Portishead
Now 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 this post


Link to post
Share on other sites
Portishead    122
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 this post


Link to post
Share on other sites
Cornstalks    7030
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 this post


Link to post
Share on other sites
visitor    643
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 this post


Link to post
Share on other sites
godsenddeath    182
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




Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this