【问题标题】:Qt c++ aggregate 'std::stringstream ss' has incomplete type and cannot be definedQt c++聚合'std :: stringstream ss'类型不完整,无法定义
【发布时间】:2012-07-29 21:23:06
【问题描述】:

我的程序中有这个函数可以将整数转换为字符串:

    QString Stats_Manager::convertInt(int num)
    {
        stringstream ss;
        ss << num;
        return ss.str();
    }

但是当我运行它时,我得到了错误:

aggregate 'std::stringstream ss' has incomplete type and cannot be defined

我不太确定这意味着什么。但是,如果您知道如何修复它或需要更多代码,请发表评论。谢谢。

【问题讨论】:

  • #include &lt;sstream&gt;
  • 顺便说一句,QString 有一个静态函数,用于从数字构造字符串。这是QString::number

标签: c++ string qt stringstream


【解决方案1】:

你可能有一个类的前向声明,但没有包含标题:

#include <sstream>

//...
QString Stats_Manager::convertInt(int num)
{
    std::stringstream ss;   // <-- also note namespace qualification
    ss << num;
    return ss.str();
}

【讨论】:

    【解决方案2】:

    就像上面写的一样,你忘记输入#include &lt;sstream&gt;

    #include <sstream>
    using namespace std;
    
    QString Stats_Manager::convertInt(int num)
    {
       stringstream ss;
       ss << num;
       return ss.str();
    }
    

    你也可以使用一些其他的方式将int转换成string,比如

    char numstr[21]; // enough to hold all numbers up to 64-bits
    sprintf(numstr, "%d", age);
    result = name + numstr;
    

    查看this!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-20
      • 2021-06-24
      • 2020-02-21
      • 1970-01-01
      • 2020-11-25
      • 2013-07-21
      • 1970-01-01
      相关资源
      最近更新 更多