-5

Since std::to_string doesn't work for me, and since I'm working on a very difficult environment at the moment (I'm working on Android using a Linux terminal emulator), I've decided to leave it broken and use a user-made function instead to replace it (I've found it online).

Here's the exact code I'm using:

#include <sstream>

namespace patch {
    template <typename T>
    std::string to_string(const T &n);
        std::stringstream stm;
        stm << n;
        return stm.str();
    }
}

I'm compiling it using these tags:

g++ -std=c++11 -g -Wall -Wextra -pedantic

And I'm getting these errors:

unknown type name 'stm'; did you mean 'tm'?
                stm << n;
                ^~~

(then a note on tm being declared somewhere in include/time.h)

expected unqualified-id
                return stm.str();
                ^

And then also an "extraneous closing brace" error for the last brace which closes the namespace brace.

As I understand it, it doesn't recognize the line stm << n; as the method operator << used on a std::stringstream object, but instead as some variable declariation for some reason.

Why exactly am I getting those errors? Is there a way to fix them? If not, what can I use to replace even this solution?

user6245072
  • 2,051
  • 21
  • 34
  • 3
    Did you include ? You're also missing an opening brace for the function and it shouldn't have a semicolon after it. – Retired Ninja Sep 15 '16 at 04:37
  • I somehow doubt that there was anything wrong with `std::to_string` to begin with. – Biffen Sep 15 '16 at 05:00
  • @Retired Ninja ouch. I copied and pasted the namespace from the declaration to the definition, and I forgot to change it I guess. Anyway yes, `` was included, and no, `std::to_string` wasn't found anywhere. – user6245072 Sep 15 '16 at 05:12
  • Works here: http://ideone.com/Rw8Lz7 Since you didn't post complete code without syntax errors it's hard to know what else might be wrong. – Retired Ninja Sep 15 '16 at 05:18
  • @Retired Ninja yeah, it worked after I fixed what you said in your first comment. I was responding to Biffen. – user6245072 Sep 15 '16 at 05:35

1 Answers1

0

Looks like you have a semi-colon where a left curly brace should be to open the function scope