Results 1 to 5 of 5

Thread: could somebody please explain const to me?

  1. #1
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default could somebody please explain const to me?

    sorry for posting what i am afraid is a pure c++-problem here, but at least it is about a qt-program:
    i have a loop going through a query and a QMap where i want to store the type and name of each column that is found. so i am trying to put the result of QSqlField::type() into my QMap, but this gives an error because it is const. Why is that? after all i had expected i am actually only copying the value into my QMap so no need to care about the original being const?
    the code looks like this:
    Qt Code:
    1. while (query.next()) {
    2. QSqlRecord rec = query.record();
    3. for (int col=0;col<rec.count();col++) {
    4. QSqlField field = rec.field(col);
    5. if (row == 0) {
    6. QMap<QString,QVariant>* currCol = &columns.value(col);
    7. currCol->insert("type",field.type()); // first error
    8. currCol->insert("name",field.name()); // second error
    9. }
    10. qDebug() << "working on row " << row << ", column " << col;
    11. }
    12. tableData.value(row) = rec; //third error
    13. row++;
    14. }
    To copy to clipboard, switch view to plain text mode 
    and in the .h i have:

    Qt Code:
    1. QMap<int,QSqlRecord> tableData;
    2. QMap<int,QMap<QString,QVariant> > columns;
    To copy to clipboard, switch view to plain text mode 

    the errors i am getting are all like this:
    passing `const QString' as `this' argument of `QString& QString::operator=(char)' discards qualifiers

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: could somebody please explain const to me?

    Please post the exact error messages and tell us which lines they correspond to.

  3. #3
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: could somebody please explain const to me?

    Quote Originally Posted by mikro View Post
    Qt Code:
    1. QMap<QString,QVariant>* currCol = &columns.value(col);
    2. currCol->insert("type",field.type()); // first error
    3. currCol->insert("name",field.name()); // second error
    To copy to clipboard, switch view to plain text mode 
    According to trolltech's docs value() method of QMap class returns const value:
    Qt Code:
    1. const T value ( const Key & key ) const
    To copy to clipboard, switch view to plain text mode 
    so you cannot assign address of this value to pointer to non-const objects.

    EDIT:
    There is also another problem. What value() returns is temporary object. It's not advisable to take and use address of temporary object.

    BTW: "Edit" button doesn't work under Opera.
    Last edited by danadam; 26th September 2006 at 00:05.
    The Wheel weaves as the Wheel wills.

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

    mikro (26th September 2006)

  5. #4
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: could somebody please explain const to me?

    yes, that was the problem - damn... i had that one before. the problem was, that at first the lines were
    Qt Code:
    1. columns.value(col).value("type") = field.type();
    2. columns.value(col).value("name") = field.name();
    To copy to clipboard, switch view to plain text mode 
    and since i splitt that a little to make it better readable i didn't look at the line above anymore, especially as i understood the errormessage to be complaining about field.type() being a const QString not about the part on the left side being const. obviously this is also the problem with the third error. Thanks

  6. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: could somebody please explain const to me?

    nothing specific to the problem but as an explaination of 'const', (among many other things)

    http://www.parashift.com/c++-faq-lite/

    K

Similar Threads

  1. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  2. Delegates and Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2006, 19:47
  3. Replies: 4
    Last Post: 24th March 2006, 22:50
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. Replies: 10
    Last Post: 10th February 2006, 00:15

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.