std::stringstream 对象在 std::to_string 替换函数中无法识别

std::stringstream object unrecognized in std::to_string replacement function

因为 std::to_string 对我不起作用,而且我目前在一个非常困难的环境中工作(我正在使用 Linux 开发 Android终端仿真器),我决定让它损坏并使用用户创建的函数来替换它(我在网上找到它)。

这是我使用的确切代码:

#include <sstream>

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

我正在使用这些标签进行编译:

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

我遇到了这些错误:

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

(然后在 include/time.h 某处声明了关于 tm 的注释)

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

最后一个关闭命名空间大括号的大括号也出现 "extraneous closing brace" 错误。

据我了解,它无法将 stm << n; 行识别为在 std::stringstream 对象上使用的方法 operator <<,而是出于某种原因将其识别为一些变量声明。

为什么我会收到这些错误?有办法修复它们吗?如果没有,我可以用什么来代替这个解决方案?

看起来你有一个分号,左花括号应该用来打开函数作用域