PDA

View Full Version : Pass pointer on Qlist as function's parameter



abshaev
7th February 2018, 15:03
Hi everybody!

I have a structure and array in QList :

struct TRadioSounding {
QString station_code; // station_code is a 5 digital code, for example 41217
QString station_name; // station_name is a 4 character code
float station_latitude; // station location
float station_longitude; // station location
float station_elevation; // station location
QDateTime datetime; // data and day time of the radio sounding
bool fitting; // flag showing that the current sounding corresponds to parameters of processing analysis
};


typedef QList<TRadioSounding> TRadioSoundings;

----------------------------------------------------------------
then I want to send pointer on QList th the function and change internal fields of some elements of QList array:


int process_all_radiousoundings(TRadioSoundings *soundings)
{
int count_fitting_soundings = 0; // number of soundings fitting to the given characteristics

for (int i = 0; i < soundings->count(); i++)
{

soundings[i]->fitting = true;
}
}


But I got an error :

error: 'TRadioSoundings {aka class QList<TRadioSounding>}' has no member named 'fitting'
soundings[i].fitting = true;
^


So how can I overpass it or correct my code ?

Added after 55 minutes:

int process_all_radiousoundings(TRadioSoundings & soundings)
{

soundings[i].fitting = true;

Lesiok
8th February 2018, 06:35
That would be good
soundings[i]->fitting = true;
if
typedef QList<*TRadioSounding> TRadioSoundings;

d_stranz
8th February 2018, 17:11
soundings[i]->fitting = true;

No. In the OP's code, TSoundings is a QList of TSounding instances. He is passing a pointer to this QList to his method. Inside the method, the proper way to access members of the QList using QList::operator[]() would be:



((*settings)[i]).fitting = true;

// or

(settings->operator[]( i )).fitting = true;


In the first case, *settings turns the pointer into a reference to a QList and therefore you can use operator[] directly. In the second case, settings remains a pointer to a QList, so the operator[] must be accessed using pointer syntax.

This syntax:



settings[i]


says that the argument is an array of QList instances (just like you would write "double * x" to point to an array of doubles) so that syntax says to return the i-th QList in the array. This will cause an access violation for any value of i beyond zero.