Results 1 to 11 of 11

Thread: QHash compile error

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QHash compile error

    Hi,
    in a sample application I have created a class member like this:

    Qt Code:
    1. QHash<int, QComboBox*> hash;
    2. hash.insert(1, comboBox_1);
    3. hash.insert(2, comboBox_2);
    4. hash.insert(3, comboBox_3);
    To copy to clipboard, switch view to plain text mode 

    but during compilation I got the following error:

    Qt Code:
    1. error: ISO C++ forbids declaration of ‘hash’ with no type
    2. error: expected ‘;’ before ‘.’ token
    To copy to clipboard, switch view to plain text mode 

    for every insert function.

    What's wrong?

    Regards
    Giuseppe CalÃ

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash compile error

    You cannot use pointers as value types with these structures. Try using QPointers, but I am not sure if it will work.

    EDIT: actually it works with pointers too. Could you provide more code, since the problem is there.


    Regards

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QHash compile error

    do you have included QHash's headerfile?
    #include <QHash>
    ...
    A mere forward declaration won't suffice in this case.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash compile error

    I think you forgot to include QHash in the cpp.

  5. #5
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QHash compile error

    But in the manual there is an example concerning the use of contains() and value() and the operator[]() in which is created data like mine:

    In general, we recommend that you use contains() and value() rather than operator[]() for looking up a key in a hash. The reason is that operator[]() silently inserts an item into the hash if no item exists with the same key (unless the hash is const). For example, the following code snippet will create 1000 items in memory:
    Qt Code:
    1. // WRONG
    2. QHash<int, QWidget *> hash;
    3. ...
    4. for (int i = 0; i < 1000; ++i) {
    5. if (hash[i] == okButton)
    6. cout << "Found button at index " << i << endl;
    7. }
    To copy to clipboard, switch view to plain text mode 
    To avoid this problem, replace hash[i] with hash.value(i) in the code above.
    QHash is included.
    Giuseppe CalÃ

  6. #6
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QHash compile error

    Here the header file:

    Qt Code:
    1. #ifndef MAINDIALOG_H
    2. #define MAINDIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QHash>
    6.  
    7. class QComboBox;
    8.  
    9. class MainDialog: public QDialog
    10. {
    11. Q_OBJECT
    12. public:
    13. MainDialog(QWidget *parent=0);
    14.  
    15. private:
    16. QComboBox *comboBox_1;
    17. QComboBox *comboBox_2;
    18. QComboBox *comboBox_3;
    19.  
    20. QHash<int, QComboBox*> hash;
    21. hash.insert(1, comboBox_1);
    22. hash.insert(2, comboBox_2);
    23. hash.insert(3, comboBox_3);
    24. };
    25.  
    26. #endif
    To copy to clipboard, switch view to plain text mode 
    Giuseppe CalÃ

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash compile error

    hash.insert(1, comboBox_1);
    hash.insert(2, comboBox_2);
    hash.insert(3, comboBox_3);
    But you cannot do this in the header, in the class declaration.
    It has to be in a function.

    Regards

  8. #8
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QHash compile error

    You're right; now the program compiles but there is a segmentation fault during execution; here the cpp file:

    Qt Code:
    1. #include <QtGui>
    2. #include "maindialog.h"
    3.  
    4. MainDialog::MainDialog(QWidget *parent)
    5. : QDialog(parent)
    6. {
    7. hash.insert(1, comboBox_1);
    8. hash.insert(2, comboBox_2);
    9. hash.insert(3, comboBox_3);
    10.  
    11. comboBox_1 = new QComboBox;
    12. comboBox_2 = new QComboBox;
    13. comboBox_3 = new QComboBox;
    14.  
    15. for(int i = 1; i < 4; i++)
    16. for(int j = 0; j < 4; j++)
    17. hash.value(i)->addItem("New Entry");
    18.  
    19. QVBoxLayout mainLayout;
    20. mainLayout.addWidget(comboBox_1);
    21. mainLayout.addWidget(comboBox_2);
    22. mainLayout.addWidget(comboBox_3);
    23. }
    To copy to clipboard, switch view to plain text mode 

    The problem seems the line using hash member.

    Bye
    Giuseppe CalÃ

  9. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash compile error

    You have to instantiate the combos BEFORE adding them to the hash.

    Regards

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QHash compile error

    you also should create the layout with new instead of using an object that dies when it reaches the end of the scope of the constructor.

  11. #11
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QHash compile error

    Ops, you're again right.

    Thanks
    Giuseppe CalÃ

Similar Threads

  1. Qtopia core 4.2.2 cross compile make error
    By smiyai18 in forum Installation and Deployment
    Replies: 2
    Last Post: 28th August 2007, 17:04
  2. qt 4.2.2 install on aix
    By try to remember in forum Installation and Deployment
    Replies: 2
    Last Post: 28th March 2007, 12:19
  3. Qt-x11-commercial-src-4.2.0-snapshot-20060824 error
    By DevObject in forum Installation and Deployment
    Replies: 4
    Last Post: 24th August 2006, 23:31
  4. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  5. Am I the only one with "make" error ?
    By probine in forum Installation and Deployment
    Replies: 1
    Last Post: 13th February 2006, 12:54

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
  •  
Qt is a trademark of The Qt Company.