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

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

本文关键字:Std 识别 对象 stringstream 替换 to string 函数      更新时间:2023-10-16

由于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;
                ^~~

(然后tminclude/time.h某处声明的注释)

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

最后一个大括号关闭了命名空间大括号,然后还有一个"多余的大括号"错误。

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

为什么我得到这些错误?有办法解决吗?如果没有,我可以用什么来代替这个解决方案呢?

看起来你有一个分号,左花括号应该是打开函数范围