0

I want to keep a static location to write to, due to multiple instantiations. I want to be able to add to the list from each instantiation. But only the first one is kept.
Not sure what to do?

Works for pointer of type char. But when I tried converting QStringList to pointer I just kept getting an error: Segmentation Fault.

*.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());
    } 
}
jdl
  • 6,151
  • 19
  • 83
  • 132
  • I think you need to declare your `QStringList` as static in the header file and not in a function. – Tyler Jandreau Jun 27 '13 at 19:03
  • Unable to make "static QStringList msgList" in the header. It errors on the build: ld returned 1 exit status – jdl Jun 27 '13 at 20:14
  • @jdl, I've updated my answer - you should add `QStringList CLASS_NAME::msgList;` in your .cpp file to avoid linking error. – Oleg Shparber Jun 27 '13 at 20:17

2 Answers2

1

You should declare your msgList static in the header. Don't forget to add QStringList CLASS_NAME::msgList; in your .cpp file.

And in your fncInit you assign empty QStringList to msgList, but anyway correct way is above.

Oleg Shparber
  • 2,732
  • 1
  • 18
  • 19
0

In your header file, you need the following code:

static QStringList msgList;

Which will make this static variable available to all functions including that header file.

When you're declaring the msgList static within a function, you're creating a local instance of it.

Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47