Can not read items. Considered various options, but for some reason they all do not work. I would be grateful for the help.

QMap <QString, QString> q2; int iter; q2.Iterator iter; for( iter = q2.begin(); iter != q2.end(); ++iter ) { qDebug() << iter.key() << " : " << iter.value(); } 

gives errors

 /home/alexandr/Documents/Job/EmailQT/EmailQT/del_duble_sql.cpp:42: error: invalid use of 'QMap<QString, QString>::Iterator' q2.Iterator iter; ^ /home/alexandr/Documents/Job/EmailQT/EmailQT/del_duble_sql.cpp:43: error: cannot convert 'QMap<QString, QString>::iterator' to 'int' in assignment for( iter = q2.begin(); iter != q2.end(); ++iter ) ^ /home/alexandr/Documents/Job/EmailQT/EmailQT/del_duble_sql.cpp:43: error: no match for 'operator!=' (operand types are 'int' and 'QMap<QString, QString>::iterator') for( iter = q2.begin(); iter != q2.end(); ++iter ) ^ /home/alexandr/Documents/Job/EmailQT/EmailQT/del_duble_sql.cpp:45: error: request for member 'key' in 'iter', which is of non-class type 'int' qDebug() << iter.key() << " : " << iter.value(); ^ /home/alexandr/Documents/Job/EmailQT/EmailQT/del_duble_sql.cpp:45: error: request for member 'value' in 'iter', which is of non-class type 'int' qDebug() << iter.key() << " : " << iter.value(); ^ 

    2 answers 2

    This is not "not working", you do not know the syntax. To access declarations within a type, use ::

     QMap <QString, QString> q2; for(QMap <QString, QString>::iterator iter = q2.begin(); iter != q2.end(); ++iter ) { qDebug() << iter.key() << " : " << iter.value() << std::endl; } 

    C ++ 11 has an excellent auto keyword, especially for such cases:

     for(auto iter = q2.begin(); iter != q2.end(); ++iter ) { qDebug() << iter.key() << " : " << iter.value() << std::endl; } 
    • 2
      In c++11 it will be even more convenient to use range-for - αλεχολυτ

    First mistake :

    /home/alexandr/Documents/Job/EmailQT/EmailQT/del_duble_sql.cpp:42: error: invalid use of 'QMap :: Iterator' q2.Iterator iter;

    The type of the variable is indicated completely wrong. You can either through the actual type of another variable:

     QMap<QString, QString>::iterator iter; 

    either (starting with C ++ 11) via decltype :

     decltype(q2)::iterator iter; 

    The second and subsequent errors:

    /home/alexandr/Documents/Job/EmailQT/EmailQT/del_duble_sql.cpp:43: error: cannot convert 'QMap :: iterator' to 'int' in assignment for (iter = q2.begin (); iter! = q2. end (); ++ iter)

    ...

    You have two variables declared with the name iter :

     int iter; q2.Iterator iter; 

    Remove the first ad.