Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert int to C++ string

Status
Not open for further replies.

chrisdash

Programmer
Mar 9, 2003
5
DE
The topic says it all, how do I convert an integer to a std::string?

I've read something about using a stringstream from chipperMDW, but that doesn't work. MS VC++ 6.0 doesn't like the stringstream:

main.cpp(34) : error C2079: 'conv' uses undefined class 'basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> >'
 
>> MS VC++ 6.0 doesn't like the stringstream:

That is not true. VC 6 supports the Standard Template Library. You just need to work a little more and understanding how to use it.

in my NetMonitor.cpp file
Code:
#include <sstream>
#include <string>
using namespace std;

also in the same file
Code:
BOOL CAboutDlg::OnInitDialog() 
{
  CDialog::OnInitDialog();
  int n = 10;
  ostringstream os;
  os << n;
  string s = os.str();
  TRACE(&quot;String is %s\r\n&quot;, s.c_str());
  return TRUE;
}

The code above is tested in a VC6 MFC application running on Windows NT 4.

if you want to use the STL i recommend obtaining a book on the subject.

good luck
-pete
 
Oh, you have to include another header to use stringstreams, thats the problem. I thought it was in <string>.
By the way, I have more than one book on the STL, I was just too stupid and lazy to use them in this case.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top