Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: What the simplest way to convert digits (int, double, float etc) to sf::String?  (Read 5144 times)

0 Members and 1 Guest are viewing this topic.

SillySquirrel

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hi guys my question in subject
I use template for it

Code: [Select]
#include <string>
#include <sstream>

template <typename T>
std::string toString(T val)
{
    std::ostringstream oss;
    oss<< val;
    return oss.str();
}

template<typename T>
T fromString(const std::string& s)
{
  std::istringstream iss(s);
  T res;
  iss >> res;
  return res;
}

I think must be easier way

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
std::to_string.

Or, if you have to use C++98 rather than modern C++, then use std::stringstream or boost::lexical_cast or sprintf or similar.

Btw: this has nothing to do with SFML - it's a basic C++ question that is really better asked elsewhere...
« Last Edit: November 26, 2014, 07:39:05 pm by Jesper Juhl »

SillySquirrel

  • Newbie
  • *
  • Posts: 3
    • View Profile
i use std::to_string before but gets error to_sting isn't std member on gcc 4.7.1 :(

I thought SFML has some typecasting method

Thx for fast anwser ;)

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Use a modern compiler/std library.
A cast doesn't convert types in such a complex manner as fx int->string - maybe in scripting languages but not in C++ with strong type safety.

SillySquirrel

  • Newbie
  • *
  • Posts: 3
    • View Profile
I update mingw for version 4.8.1 TDM but problem with std::to_string() not resolves
Here i found some fix (not work for me actually)

PS -std=c++11 of course

PPS I found the problem. IDE global compile vars set c++0x at the end of command line options
Code: [Select]
mingw32-g++.exe -std=c++11 -Wzero-as-null-pointer-constant -std=c++0x -ID:\SFML\INCLUDE -ID:\SFML\lib -c D:\TEST\main.cpp -o obj\Debug\main.o
LOL
« Last Edit: November 26, 2014, 10:28:20 pm by SillySquirrel »