PDA

View Full Version : How to create a QList of QPointers?



dobedidoo
27th November 2009, 09:20
Hi

I'm trying to create a QList of QPointers, something like this:


QList<QPointer<someClass>>* myQPointerList_;

However, this syntax does not seem to be good. I get errors like:

1>d:\testproj\mainWindow.h(282) : error C2947: expecting '>' to terminate template-argument-list, found '<'
1>d:\testproj\mainWindow.h(282) : error C2143: syntax error : missing ';' before '>>'
1>d:\testproj\mainWindow.h(282) : error C2238: unexpected token(s) preceding ';'
I use the (Win32) MSVC 2005 compiler.

So, is there a way to handle this (that is, to create the QList of QPointers that I would like to use)?

Lykurg
27th November 2009, 09:27
put a space between the brackets:

QList<QPointer<someClass> > myQPointerList_; and allocate it on the stack, not on the heap.

dobedidoo
27th November 2009, 14:46
Well, I'm quite a beginner in C++/Qt, so I'm not sure I fully understand what that means - to "allocate it on the stack, not on the heap" . But, one thing is that I need this QList of QPointers to be accessible/recognized by several methods/"functions" in my main class, which is why I thought I needed to declare it in the header file.

If I just do

QList<QPointer<someQClass> > myQPointerList
in the constructor of my main class, then myQPointerList will not be "known" by the other methods/"functions" in my main class - right? Or am I wrong - maybe it can be?

Lykurg
27th November 2009, 14:59
Well, I'm quite a beginner in C++/Qt,
...then please use the Newbie section. We also read and answer thread there and know that the users aren't so good with C++/Qt. Then we answer in more detail.

so I'm not sure I fully understand what that means - to "allocate it on the stack, not on the heap".

I wrote that because you had an asterisk in your definition. So for you everything is fine if you use it like:
// header file
class MyClass
{
//...
private:
QList<QPointer<someQClass> > myQPointerList;
}

// and in the implementation you can use it everywhere

void MyClass::someFunction()
{
myQPointerList.append(new someQClass);
// etc.
}

nish
27th November 2009, 15:18
this not related to QList ... this is simple C++ concept... if your declare any variable in constructor then it is not possible to access this outside..

so make it a member of the class..

"allocate it on the stack, not on the heap" means
int a; <-- stack, a will die after its scope, that may be function or object
int * a = new int; <--heap // a will remain in memory until delete is called

EDIT: I m TOO late.