PDA

View Full Version : QList can't be returned



baluk
30th September 2010, 13:00
I have a problem when returning QList from a fucntion. I have two classes in which one class returns a QList from a fucntion and the other class catches this list. But I get null List returned though list in the other class has values. Below is my classes overview.



class A
{
classB *b;
void f()
QList<QString>* list = b->function();
qDebug() << list.at(0); // getting error
}

class B
{
QList<QString> Blist;
void list()
{
int i=0;
.....
Blist.insert(i,"value");
qDebug() << Blist.at(i);
i++;
}
QList<QString>* function()
{
qDebug() << Blist.at(0) ; // printing the value at index 0
return &Blist;
}
}


Can any one please tell me why I am getting null list returned.

Thank You,
Baluk

tbscope
30th September 2010, 13:22
The problem is most likely not your function but the fact that you do not initialise b!

Before you use

b->function()

You need to

b = new classB;

squidge
30th September 2010, 13:25
list.at is invalid. It should be list->at, as your using a pointer.

Oh, and ensure you allocate the memory to, or just remove that asterix.

wysota
30th September 2010, 13:26
Returning a pointer to an owned list in an object oriented language (and not only there) is probably not the wisest decision.

tbscope
30th September 2010, 13:27
list.at is invalid. It should be list->at, as your using a pointer.
But that should result in a compile error.

baluk
30th September 2010, 13:46
I have initialized the class. I don't have any compile errors. Only a runtime error. And I fallowed the same procedute to QMap also in my other application. It is working fine.

tbscope
30th September 2010, 13:48
The following code contains a lot of errors (it does not even compile) can you post the real code please?


class A
{
classB *b;

void f()

QList<QString>* list = b->function();
qDebug() << list.at(0); // getting error
}

baluk
30th September 2010, 13:55
My app has 100 lines of code. thats why I have just given the basic view. I will try to write the new app and post it here.

tbscope
30th September 2010, 14:33
Not the whole code. Just the parts of class A, and more specifically, your function f.
Like you wrote it, it does not work.

baluk
30th September 2010, 18:00
Here is the code sample.


#include "xmldata.h"
class calculateHelper : public QMainWindow
{
Q_OBJECT

public:
explicit calculateHelper(QWidget *parent = 0);
~calculateHelper();
private:
xmlData *xml;
QList<QString>* list1;
}

Thank You,
baluk
calculateHelper::calculateHelper(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::calculateHelper)
{
xml = new xmlData();
}
void calculateHelper::insertList()
{

ui->tableWidget->clear();

this->fDetails = xml->getInfo();
list1 = xml->returnList();
}

QList<QString>* xmlData::returnList()
{
&list;
}


Thank You,
Baluk

tbscope
30th September 2010, 19:03
Ok, I didn't see this before:


QList<QString>* xmlData::returnList()
{
&list;
}


QList<QString>* function()
{
qDebug() << Blist.at(0) ; // printing the value at index 0
return &Blist;
}

are both wrong. The first more than the second.

Do you know the meaning of &list? what is the difference with *list?

baluk
1st October 2010, 07:05
&list means returning the address of list in the above contest but I forgot to write return statement. *list is a pointer variable pointing to some arbitary memory location.

tbscope
1st October 2010, 07:38
&list means returning the address of list in the above contest but I forgot to write return statement. *list is a pointer variable pointing to some arbitary memory location.

No, that is wrong.

& is the reference operator
* is the dereference operator.


int i = 5;
int *j = &i;
int k = *j;

In your case your function returns a pointer to a qlist, but you reference the pointer again.

baluk
1st October 2010, 07:43
My code is now working, I am sorry that I was overlooked about writing "return" statement. As from your example I am doing this
int *j = &i; in my code. Isn't it.

tbscope
1st October 2010, 07:49
My code is now working, I am sorry that I was overlooked about writing "return" statement. As from your example I am doing this
int *j = &i; in my code. Isn't it.

You are actually doing:

QList<QString> *list;

...

QList<QString> *Class::getList()
{
return &list;
// list already is a pointer, so you do not need to reference it.
}

// If you have this:
int a = 1;
int b = 2;
int *ap = &a;
int *bp = &b;

// You do this:
int *c = &ap;
// While it should be:
int *c = ap;

squidge
1st October 2010, 08:12
To prevent this from happening in the future, you should try some tools like 'lint' which check your source code for various types of errors (such as not returning a value in a function declared as returning a value)

baluk
1st October 2010, 08:16
"list" is not a pointer variable but just a variable. "list1" is a pointer variable. In my given code do you see that i used two classes "calculatehelper" and "xmlData". I just showed you the first "class declaration" not the second one.
QList<QString>* xmlData::returnList()
{
return &list;
} this function belongs to second class in which I declared the list variable as
QList<QString> list;. Sorry for naming conventions and made it confused.

Thank You,
baluk.

baluk
1st October 2010, 09:53
Hi Can you provide any link to how to use this "lint" tool. I tried goggling but couldn't find any usefull link. I want to use this one in my Qt.

squidge
1st October 2010, 12:42
Apologies, I confused the names. 'lint' is more for C code (It will work for C++, but you have to pay), for C++ code you might want to look at cppcheck, which is free and open source: http://en.wikipedia.org/wiki/Cppcheck

and ensure to add '-Wall' to the command line for the GCC compiler.