由于多个实例化,我希望保留要写入的静态位置。我希望能够从每个实例化中添加到列表中。但只保留了第一个。
不知道该怎么办?
适用于char类型的指针。但是,当我尝试将QStringList转换为指针时,我一直收到一个错误:分段错误。
*.h
QStringList msgList;*.cpp
fncInit(){
static QStringList MessageList;
msgList = MessageList;//keep the location constant for all new instantiations
}
fncBuild(QString strMessage){
MessageList.append(strMessage); //if I use a pointer QStringList through out, I get Segmentation Fault.
}
fncPrintf(){
for(int i; i < msgList.count(); i++){
printf("%d) %s", i, msgList.at(i).toStdString().c_str());
}
}发布于 2013-06-27 19:04:11
您应该在标头中声明msgList静态。不要忘记在您的QStringList CLASS_NAME::msgList;文件中添加.cpp。
在您的fncInit中,您将空的QStringList分配给msgList,但是无论如何,正确的方法在上面。
发布于 2013-06-27 19:23:31
在头文件中,需要以下代码:
static QStringList msgList;
这将使这个静态变量对包括该头文件在内的所有函数都可用。
当您在函数中声明msgList静态时,您将创建它的本地实例。
https://stackoverflow.com/questions/17351148
复制相似问题