Qt C ++聚合'std :: stringstream ss'类型不完整,无法定义


97

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

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

但是,无论何时我运行此命令,我都会收到错误消息:

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

我不太确定那是什么意思。但是,如果您知道如何解决它或需要更多代码,请发表评论。谢谢。


48
#include <sstream>
Managu

1
顺便说一句,QString具有用于从数字构造字符串的静态函数。这是QString :: number
cgmb

Answers:


153

您可能具有该类的前向声明,但尚未包含标头:

#include <sstream>

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

9

就像上面写的一样,您忘记输入 #include <sstream>

#include <sstream>
using namespace std;

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

您还可以使用其他方式将其转换intstring,例如

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

检查一下!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.