Results 1 to 7 of 7

Thread: QList<A*> indexOf

  1. #1
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Default QList<A*> indexOf

    Hi,

    can someone explain this compiler error below?
    Qt Code:
    1. #include <QList>
    2.  
    3. class A
    4. {
    5.  
    6. public:
    7. A(){ _list = new QList<A*>(); }
    8. ~A(){ delete _list;}
    9. int indexOfChild(const A* x) const { return _list->indexOf( x, 0); }
    10.  
    11. private:
    12. QList<A*> *_list;
    13. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. a.cpp: In member function `int A::indexOfChild(const A*) const':
    2. a.cpp:9: error: invalid conversion from `const A*' to `A*'
    3. a.cpp:9: error: initializing argument 1 of `int QList<T>::indexOf(const T&,
    4. int) const [with T = A*]'
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList<A*> indexOf

    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.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QList<A*> indexOf

    The list contains non-constant pointers and you are passing a constant pointer for QList::indexOf() method.

    Try:
    Qt Code:
    1. int indexOfChild(A* x) const { return _list->indexOf( x, 0); }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QList<const A*>
    To copy to clipboard, switch view to plain text mode 
    which way fits better for your need..

  4. The following user says thank you to jpn for this useful post:

    Moppel (3rd March 2006)

  5. #4
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: QList<A*> indexOf

    That would not work since the calling function passes a const pointer:

    Qt Code:
    1. QModelIndex AModel::itemToIndex(const A *x, int column) const{
    2. const A *parent = x->parent();
    3. int row = parent->indexOfChild(x);
    4. ...
    To copy to clipboard, switch view to plain text mode 
    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.

    Qt Code:
    1. #include <QList>
    2.  
    3. class A
    4. {
    5.  
    6. public:
    7. A(A* parent = 0){ _list = new QList<const A*>(); _parent = parent;}
    8. ~A(){ delete _list;}
    9. int indexOfChild(const A* x) const { return _list->indexOf( x, 0); }
    10.  
    11. private:
    12. int i;
    13. A *_parent;
    14. QList<const A*> *_list;
    15. };
    To copy to clipboard, switch view to plain text mode 

    I still don't really understand the previous error.
    QList's method is declared like this.
    Qt Code:
    1. int QList::indexOf ( const T & value, int from = 0 ) const
    To copy to clipboard, switch view to plain text mode 
    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.

  6. #5
    Join Date
    Mar 2006
    Posts
    22
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QList<A*> indexOf

    Simple, quick solution:
    Qt Code:
    1. int indexOfChild(const A* x) const { return _list->indexOf( (A *)x, 0); }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList<A*> indexOf

    Quote Originally Posted by dublet
    Simple, quick solution:
    Simple, quick and dirty It breaks the "const" modifier.

  8. #7
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList<A*> indexOf

    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.

Similar Threads

  1. help on QList<QTableWidgetItem*>
    By roncriss in forum Newbie
    Replies: 2
    Last Post: 24th November 2008, 10:19
  2. QList<char*> dies
    By thomaspu in forum Qt Programming
    Replies: 11
    Last Post: 15th March 2008, 23:47
  3. QList<myObject*> myObjectList count() error
    By morty in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2006, 16:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.