【问题标题】:std::stringstream object unrecognized in std::to_string replacement function [closed]std::to_string 替换函数中无法识别的 std::stringstream 对象[关闭]
【发布时间】:2016-09-15 04:35:52
【问题描述】:

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

然后还有一个“无关的右大括号”错误,用于关闭命名空间大括号的最后一个大括号。

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

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

【问题讨论】:

  • 您是否包含 ?您还缺少该函数的左大括号,并且它后面不应有分号。
  • 我不知何故怀疑std::to_string 有什么问题。
  • @Retired Ninja 哎哟。我将名称空间从声明复制并粘贴到定义中,但我猜我忘了更改它。无论如何,是的,&lt;string&gt; 被包括在内,不,std::to_string 在任何地方都找不到。
  • 在这里工作:ideone.com/Rw8Lz7 由于您没有发布没有语法错误的完整代码,因此很难知道还有什么问题。
  • @Retired Ninja 是的,在我修复了您在第一条评论中所说的内容后,它起作用了。我在回复 Biffen。

标签: c++ c++11 tostring stringstream


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    相关资源
    最近更新 更多