simple variable typecast

how do you type cast int to string?
You don't.

Perhaps you want to convert an integer to a std::string? If so, use std::stringstream as shown here:
http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/ (found via Google!)
closed account (1yR4jE8b)
Use std::stringstream :

1
2
3
4
5
6
7
8
9
10
11
#include <sstream>
#include <iostream>

int main() {
  int x = 10;
  std::stringstream y;
  y << x;
  std::cout << y.str() << std::endl;

  return 0;
}
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main() 
{
	int x = 10;
	std::string s = std::to_string(x);
	std::cout << s << std::endl;
}
when I use stringstream I get a weird error and my compiler doesnt recognize to_string(). For stringstream the error is "implicit instantiation of undefined template 'std::stringstream<char........"

and ftw I triend streamstream before using that exact article found on google and got this error.
Last edited on
did you #include <sstream> ?
thank you peter .. that was it
Until c++11 is the dominant standard, it would be cool if people tagged c++11 specific code!
Topic archived. No new replies allowed.