PDA

View Full Version : Add QThread objects to QLinkedList



Msnforum
25th February 2010, 01:59
I'm new here on the forum and I need some answers as to what I have done wrong in my coding.
I usually code in Java, C# and some extend of C++ so I might have mixed something out.

What I'm trying to do is to create a class that have stores threads of a custom class into a LinkedList.
The error that I came out is Error 1 error C2248: 'QObject::QObject' : cannot access private member declared in class 'QObject' c:\qt\4.4.3\src\corelib\thread\qthread.h 132


I myself think that the error is caused by the incorrect use of the LinkedList append method in the listClass add method, any help would be appreciated.

This is not the complete/original code but it's fairly similar except having the includes.



class CustomObj : public QThread
{
Q_OBJECT
public:
CustomObj();
protected:
void run();
}

CustomObj::CustomObj()
{
}

void CustomObj::run()
{
}




class listClass
{
public:
listClass();
void add(CustomObj cus);
private:
QLinkedList<CustomObj> *list;
}

listClass::listClass()
{
list = new QLinkedList<CustomObj>;
}

void listClass::add(CustomObj cus)
{
list->append(cus);
}

franz
25th February 2010, 08:01
You cannot copy QObjects (you are trying to access the private copy operator). Keep pointers to the objects in the list instead of the object itself.

Msnforum
25th February 2010, 10:24
Cheers, I knew something was wrong there. I tried changing it to a pointer but it doesn't match the parameter required for the QLinkedList append method.



void listClass::add(CustomObj cus)
{
CustomObj *pointer = &cus;
list->append(pointer);
}


I know it's not the right place to ask this general C++ question but could you please provide an example or a code snippet for this typical question.

wagmare
25th February 2010, 10:33
assign your QList like this

QLinkedList<CustomObj *> wlist

pitonyak
25th February 2010, 16:32
This code concerns me:

void listClass::add(CustomObj cus)
{
CustomObj *pointer = &cus;
list->append(pointer);
}
My concern is that you understand when an object is destroyed and who owns the pointer. I do not know you well enough to know if you understand these issues or not. If you do not, be certain to say so. Consider what happens if I create an object and then add it to a list or simply take the pointer of the object. For example:


MyObj* Pointer = NULL;
if (Pointer == NULL)
{
MyObj x;
Pointer = &x;
}

In this example, at the end of the if statement, the variable x goes out of scope and the destructor is called. This means that Pointer is left pointing at a variable that is no longer valid. On the other hand, if you have a list of pointers, there can always be an issue with who owns the pointer and who should delete it. As long as you understand these issues, then there is no problem.