PDA

View Full Version : QList<A*> indexOf



Moppel
3rd March 2006, 20:51
Hi,

can someone explain this compiler error below?


#include <QList>

class A
{

public:
A(){ _list = new QList<A*>(); }
~A(){ delete _list;}
int indexOfChild(const A* x) const { return _list->indexOf( x, 0); }

private:
QList<A*> *_list;
};




a.cpp: In member function `int A::indexOfChild(const A*) const':
a.cpp:9: error: invalid conversion from `const A*' to `A*'
a.cpp:9: error: initializing argument 1 of `int QList<T>::indexOf(const T&,
int) const [with T = A*]'


Why takes the conversion from 'const A*' to 'A*' place?
if T = A*
this is
int QList<A*>::indexOf(const A*&, int) const
But A*& ??

References and pointers....

Can someone enligthen me, please?

Thanks,

Moppel

wysota
3rd March 2006, 21:02
You are trying to pass a pointer to a non-const object to a method which requires a const object. The error (with pointer and references) is just a result of trying to find a match for aguments. Compiler tries different combinations and maybe a reference is the last thing it tries, so it fails on it and returns such a combination. Either remove the "const" modifier from the method parameter or make sure you pass a const object.

jpn
3rd March 2006, 21:05
The list contains non-constant pointers and you are passing a constant pointer for QList::indexOf() method.

Try:


int indexOfChild(A* x) const { return _list->indexOf( x, 0); }

or


QList<const A*>

which way fits better for your need..

Moppel
3rd March 2006, 21:27
That would not work since the calling function passes a const pointer:



QModelIndex AModel::itemToIndex(const A *x, int column) const{
const A *parent = x->parent();
int row = parent->indexOfChild(x);
...

Of course I could get rid of all the const. But I don't want to do that.

But it does work if I use the QList like this.



#include <QList>

class A
{

public:
A(A* parent = 0){ _list = new QList<const A*>(); _parent = parent;}
~A(){ delete _list;}
int indexOfChild(const A* x) const { return _list->indexOf( x, 0); }

private:
int i;
A *_parent;
QList<const A*> *_list;
};


I still don't really understand the previous error.
QList's method is declared like this.


int QList::indexOf ( const T & value, int from = 0 ) const

The method will not modify T, or how do I have to read the const T& here?

I guess I have to get my Bruce Eckel's out again.

dublet
6th March 2006, 09:37
Simple, quick solution:

int indexOfChild(const A* x) const { return _list->indexOf( (A *)x, 0); }

wysota
6th March 2006, 10:32
Simple, quick solution:

Simple, quick and dirty :) It breaks the "const" modifier.

mhennings
8th August 2017, 14:17
This is a very valid question.
Why not search a non-const QList for a const value? Searching won't change anything in the list, the constness of operator==() ensures that.
Even more, if t would not be passed by const reference but by const value, e.g.:
int indexOf(T const t, int from = 0) const;
the non-const -> const cast would happen automatically in the comparison operator.

In fact, I'm wondering why QList / QVector doesn't use T const* n and T const *e as the iterators.