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

Author Topic: How do I display a variable?  (Read 24911 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
How do I display a variable?
« Reply #15 on: June 28, 2011, 09:15:21 pm »
You are almost certainly using it wrong, I have worked with std::stringstream on MSVC 2008 and I have always been calling the member function str(). By the way, you should not overuse using namespace, and completely avoid it in header files.

Can you show a minimal, complete code example with the error?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
How do I display a variable?
« Reply #16 on: June 28, 2011, 09:21:32 pm »
You should only have to include <sstream>.

If you are writing oss.string(), then you can't possibly be using std::string.
(Where oss is your std::ostringstream).

But it is definitely str(), anyway.

On the subject of
Code: [Select]
using namespace, IMO it's best to put it in limited scope, for example inside your main function (yes, you can do that ;) ). Then it will only bring stuff in std into scope within main. Of course, sometimes it's easier to put it globally, but that is risky.

@Nexus Beat me to it ^^

Cuban-Pete

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
How do I display a variable?
« Reply #17 on: June 28, 2011, 09:23:08 pm »
The smallest and worst code where it still works... :P

Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <string>
#include <iostream>
#include <sstream>

using namespace std;

class MakeString  
{
   public:
      template <typename T>
      MakeString& operator<< (const T& value)
      {
         mStream << value;
         return *this;
      }
       
      operator std::string() const
      {
         return mStream.str();
      }
       
   private:
      std::ostringstream mStream;
};


int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "test", sf::Style::Close);

window.SetFramerateLimit(60);

   while (window.IsOpened())
{
        // Process events
        sf::Event event;
        while (window.PollEvent(event))
        {
            // Close window : exit
            if (event.Type == sf::Event::Closed)
                window.Close();

            // Escape key : exit
            if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape))
                window.Close();
        }

sf::Text text( string(MakeString() << window.GetInput().GetMouseX() << " " << window.GetInput().GetMouseY() ) );
text.SetPosition(250.f, 450.f);
text.SetColor(sf::Color(255, 255, 255, 170));
window.Draw(text);

window.Display();
    }

    return 0; //EXIT_SUCCESS
}

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
How do I display a variable?
« Reply #18 on: June 28, 2011, 09:26:23 pm »
The boost library provides a boost::lexical_cast function that implements this functionality.

Code: [Select]

#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>
     
int main()
{
     int ntest = 10;
     std::string test = boost::lexical_cast<std::string>(test);
     std::cout << test << std::endl;
     return 0;
}
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
How do I display a variable?
« Reply #19 on: June 28, 2011, 09:26:30 pm »
So it works there? Then you must have changed something in your actual code. Compare the two and see what you can find.

Cuban-Pete

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
How do I display a variable?
« Reply #20 on: June 28, 2011, 09:29:08 pm »
Yes works for me, but you guys were wondering why it only worked with string() and not str().

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
How do I display a variable?
« Reply #21 on: June 28, 2011, 09:36:01 pm »
But you have used str():
Code: [Select]
return mStream.str();

This is what we were talking about. The point were you have used 'string' below, it is std::string, as it should be, because you are telling the compiler to construct an std::string from a 'MakeString', which it can do by virtue of this function:
Code: [Select]

      operator std::string() const
      {
         return mStream.str();
      }

Cuban-Pete

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
How do I display a variable?
« Reply #22 on: June 28, 2011, 09:44:32 pm »
hehe, misunderstanding. I was aiming at this part:

Quote from: "Nexus"

Usage:
Code: [Select]
int main()
{
    int health = 100;
    sf::String str(MakeString() << "Health: " << health);
}


there I use string() instead of str()

Sorry for wasting time...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
How do I display a variable?
« Reply #23 on: June 28, 2011, 10:34:47 pm »
Quote from: "Cuban-Pete"
there I use string() instead of str()
I don't see why str is a problem, unless you have a macro called like this.

So, instead of guessing all the time... Either show us a complete code where the error occurs, or let's forget about this ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Haikarainen

  • Guest
How do I display a variable?
« Reply #24 on: July 12, 2011, 09:21:09 am »
I've noticed that on some systems/compilers/environments/whatever, you need to include "string.h" and not "string" to get all of its functions. Dont ask me why. Personally I've only came across this using GCC

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
How do I display a variable?
« Reply #25 on: July 12, 2011, 11:25:31 am »
Maybe the OP tried to do this:

Code: [Select]

...
...
sf::Text text( str(MakeString() << window.GetInput().GetMouseX() << " " << window.GetInput().GetMouseY() ) );
...
...


which wouldn't work, and would probably get the "error C3861: 'str': identifier not found"

:)

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
How do I display a variable?
« Reply #26 on: July 14, 2011, 07:53:12 pm »
Quote
I've noticed that on some systems/compilers/environments/whatever, you need to include "string.h" and not "string" to get all of its functions. Dont ask me why. Personally I've only came across this using GCC


<string.h>  (aka <cstring>) and <string> are two completely different headers.

<cstring> has things like strcpy, strcmp, etc
<string> has the std::string class

 

anything