Qt c + + 聚合“ std: : stringstream ss”的类型不完整,无法定义

在我的程序中有一个函数可以把整数转换成字符串:

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

但是当我运行这个程序时,我会得到一个错误:

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

我不太确定那是什么意思。但是,如果你知道如何修复它或需要任何更多的代码,请只是注释。谢谢。

147039 次浏览

You probably have a forward declaration of the class, but haven't included the header:

#include <sstream>


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

Like it's written up there, you forget to type #include <sstream>

#include <sstream>
using namespace std;


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

You can also use some other ways to convert int to string, like

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

check this!