Results 1 to 16 of 16

Thread: About a stange issue with a map variable inside a const function

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default About a stange issue with a map variable inside a const function

    I have a map<int<map<int,char> > variable.
    It is declared at the private section of my .h file :

    map<unsigned int,std::map<unsigned int,char> > map__type;
    I also have :
    vector<string> vecHheader;

    I use it in this function with sufix const keyword
    myfunction (bla bla) const

    Ok I can return vector information but not map ???
    ( I have the const error : ... bla bla bla dicard qualifiers.)

    Whats happen ? I can not use map into a const functions ? I want only to returning information !

    Any idea ? Thanks .


    And , If I made mymap mutable... What can I expect from mymap or const function perfomance ?
    Last edited by tonnot; 14th April 2011 at 16:26.

  2. #2
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Can I work with std::map::iterator ?

    I'm trying to use map::iterator code I found on the web but... code completion no offers me ->first neither ->second ....

    And when compile I have an error and recomendation to use _rb_tree_iterator?

    And... if I use std::map at .h file I have not completion code for maps and vectors, I have to use 'using namespace std'.

    Any idea ?
    (I'm using QT+mingw)
    Last edited by tonnot; 14th April 2011 at 18:23.

  3. #3
    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: About a stange issue with a map variable inside a const function

    Sorry but what are you asking about? Constness is constness, regardless of types/classes you use.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can I work with std::map::iterator ?

    Qt does not stop you using any other part of C++, including the STL. I have found the code completion in Qt Creator to be a little hit and miss with the STL iterator constructs though (.first and .second have never worked for me).

    The namespace stuff is generic C++. Including <map> defines a map class in the std namespace. You can access that explicitly as std::map. If you wish, you may make the entire content of the std namespace available in global scope with "using namespace std;". Then you can access std::map as just map.

    What does the code that generates the error and the error itself look like?

  5. #5
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: About a stange issue with a map variable inside a const function

    I'm asking for the quesion :
    'I can return vector information but not map ???
    (the function in which I use this is a reimplemented QVariant() tablemodel::data() const
    Thanks

  6. #6
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can I work with std::map::iterator ?

    I have :
    Qt Code:
    1. map<char,int> mymap;
    2. map<char,int>::iterator it;
    3.  
    4. mymap['a']=50;
    5. mymap['b']=100;
    6. mymap['c']=150;
    7. mymap['d']=200;
    8.  
    9. it=mymap.find('b');
    10. cout<<it->second<< endl;
    To copy to clipboard, switch view to plain text mode 

    It works at the constructor of my custom tablemodel constructor but not at the reimplemented function mycustomtablemodel::data () const

    An this is the error and the recomendation to use _rb_tree....

    error: no match for 'operator=' in '((const A_model*)this)->A_model::it = ((const A_model*)this)->A_model::mymap.std::map<_Key, _Tp, _Compare, _Alloc>::find [with _Key = char, _Tp = int, _Compare = std::less<char> ....
    note: candidate is: std::_Rb_tree_iterator<std:air<const char, int> >& std::_Rb_tree_iterator<std:air<const char, int> >:perator=(const std::_Rb_tree_iterator<std:air<const char, int> >&)


    Simply I cannot use 'map' code I found on the web.
    I am at dead point with this....


    (Something happens with colon colon p , forum message imagine that it is an smile... and it is colon colon pair )

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can I work with std::map::iterator ?

    What happens if you change the name of mymap to something else?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can I work with std::map::iterator ?

    I dont understand you. ( change the name of mymap to something else? )

    I have fixed the problem with a poor solutiuon: make my maps mutable, and use mymaps[][].
    It is a bad approach, but I dont know how to use the rb_tree_ and ... I dont understand why it happens with maps and however I can use list, vectors, etc inside this const function....

    Any more ideas ? Thanls

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can I work with std::map::iterator ?

    I dont understand you. ( change the name of mymap to something else? )
    I thought there might be some type conflicst since the error says you are using 'const A_model*' while in your code you are using char and int.

    I have fixed the problem with a poor solutiuon: make my maps mutable, and use mymaps[][].
    That means you have a 'const' problem.
    Show the real code you are using.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #10
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can I work with std::map::iterator ?

    Thanks for your time HF
    This is the complete .h file and the data function of .cpp. ( a piece o cpp file, becasue it is full of comments and )
    As I said, now (with the mutable change) it works, but I dont understand the rb_tree advise if I dont use mutable.
    (And also I dont understand why I can use vector without problems inside this const functions)

    A_model.h
    Qt Code:
    1. #include <QtGui/QTableView>
    2. #include <QtGui/QHeaderView>
    3. #include <QAbstractTableModel>
    4. #include <QItemSelection>
    5. #include <QObject>
    6. #include <string>
    7. #include <map>
    8. #include <vector>
    9. using namespace std;
    10.  
    11. class A_model : public QAbstractTableModel
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. A_model(int rows, int cols, QObject *parent=0);
    17.  
    18. //reimplemented
    19. int rowCount(const QModelIndex &parent) const;
    20. int columnCount(const QModelIndex &parent) const;
    21. QVariant data(const QModelIndex &index, int role) const;
    22. QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    23. virtual bool setHeaderData(int section,Qt::Orientation orientation,const QVariant &value);
    24. Qt::ItemFlags flags(const QModelIndex &index) const;
    25. bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole);
    26. bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex());
    27. bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex());
    28. bool insertColumns(int column, int count, const QModelIndex &index);
    29.  
    30. // custom functions
    31. void set_cols(int cols) ;
    32. void set_rows(int rows) ;
    33. int M_ROWS, M_COLS, M_ROW, M_COL;
    34.  
    35. private:
    36.  
    37. QList< QPair<QString, QString> > listOfPairs;
    38. vector<int> i_vector; //1
    39. vector<long> l_vector; //2
    40. vector<float> f_vector; //3
    41. vector<double> d_vector; //4
    42. vector<std::string> s_vector; //5
    43.  
    44. mutable map<unsigned int,map<unsigned int,char> > map__type;
    45. mutable map<unsigned int,map<unsigned int,unsigned int> > map_index;
    46.  
    47. mutable map<unsigned int,map<unsigned int,char> >::iterator map__type_i1;
    48. mutable map<unsigned int,char>::iterator map__type_i2;
    49.  
    50.  
    51.  
    52. vector<string> vecHheader;
    53. vector<string> vecVheader;
    54. };
    To copy to clipboard, switch view to plain text mode 

    And the cpp.

    Qt Code:
    1. QVariant A_model::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. { return QVariant();}
    5.  
    6. if (index.row() >= M_ROWS || index.row() < 0)
    7. { return QVariant();}
    8.  
    9. if (role == Qt::DisplayRole) {
    10.  
    11. unsigned int rr=(unsigned int)(index.row());
    12. unsigned int cc=(unsigned int)(index.column());
    13. switch (map__type[rr][cc])
    14. {
    15. case 1:
    16. break;
    17. case 2:
    18. break;
    19. case 3:
    20. break;
    21. case 4:
    22. break;
    23. case 5:
    24. return W_utiles().To_qstring(map_index[rr][cc])+" "+ QString().fromStdString(s_vector.at(map_index[rr][cc]));
    25. break;
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    I use a vector so have the data for my headers. And can use it at this function without problem
    Qt Code:
    1. QVariant A_model::headerData(int section, Qt::Orientation orientation, int role) const
    2. ....
    3. return vecHheader[section];
    4. .....
    To copy to clipboard, switch view to plain text mode 
    (This is because I say I can use vector without problems but not map)

    Help.

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can I work with std::map::iterator ?

    Two things:
    1. As usual, I read your post to quickly, but the answer is there if one reads carefully:
    error: no match for 'operator=' in '((const A_model*)this)->A_model::it = ((const A_model*)this)->A_model::mymap.std::map<_Key, _Tp, _Compare, _Alloc>::find [with _Key = char, _Tp = int, _Compare = std::less<char> ....
    note: candidate is: std::_Rb_tree_iterator<std:air<const char, int> >& std::_Rb_tree_iterator<std:air<const char, int> >erator=(const std::_Rb_tree_iterator<std:air<const char, int> >&)
    Now you using mutable got me on the right track - namely, const problem.

    Which has nothing to do with STL, but with your funciton definition:
    QVariant A_model::headerData(int section, Qt::Orientation orientation, int role) const
    'const' means your method does not alter the content of the class, but you ARE trying to.
    That is the reason your iterator complains, he expects const types, where as you gave it char and not const char.

    If you remove the const from your method definition/declaration, the error should go away.

    That is also the reason your vector works, since you are just returning , not altering, so there your method is correctly const
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  12. #12
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can I work with std::map::iterator ?

    But I want to use map_index[rr][cc]) to return data.....

  13. #13
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can I work with std::map::iterator ?

    So?
    Did you read (and understand) my last post?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  14. #14
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can I work with std::map::iterator ?

    You say :
    'const' means your method does not alter the content of the class, but you ARE trying to.
    Ok, I understand but I only wnat to return data when the tablemodel is asking me for it.
    I see a lot of examples using vectors, lists, Qlists, etc
    And the question is ,MAP has a different behavior and MAP[][] is interpretated as a change ?
    Thanks and thanks for your patience.

  15. #15
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can I work with std::map::iterator ?

    I see.
    But you also have the syntax semantics that you need to respect, and std::map is a bit special too.
    Since a const method is not allowed to change things, using non const expressions is not allowed.
    Since your map is defined with non const types, you get a problem.
    In the case of std::map however, you really can't avoid this, since the operator[] creates a new entry if the entry does not exist, and it would not be able to do it, if it was const.
    So in this case your solution is to define your method not as const - OR - use sts::map::find() instead of [].
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  16. The following user says thank you to high_flyer for this useful post:

    tonnot (15th April 2011)

  17. #16
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can I work with std::map::iterator ?

    Ok, and thank you very much

Similar Threads

  1. Emit signal from const function
    By waynew in forum Qt Programming
    Replies: 9
    Last Post: 22nd August 2010, 13:10
  2. emitting signals in const function
    By maxel in forum Qt Programming
    Replies: 3
    Last Post: 19th October 2008, 15:05
  3. Replies: 6
    Last Post: 7th October 2008, 12:14
  4. subclassed const function problem
    By qtneuling in forum Newbie
    Replies: 8
    Last Post: 22nd June 2008, 02:52
  5. const function parameter problems
    By stevey in forum General Programming
    Replies: 3
    Last Post: 18th December 2006, 22:22

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.