Results 1 to 2 of 2

Thread: Problems using QMap

  1. #1
    Join Date
    Mar 2006
    Posts
    17
    Thanks
    2
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Problems using QMap

    I am trying to create a QMap as a seperate .h .cpp pair because it is a really long list, but doing so generates errors.

    Here is the .h
    Qt Code:
    1. #ifndef XMLSQLMAP_H
    2. #define XMLSQLMAP_H
    3.  
    4. #include <qmap.h>
    5. #include <qstring.h>
    6.  
    7.  
    8. QMap<QString,QString> xmlsqlmap;
    9.  
    10. #endif
    To copy to clipboard, switch view to plain text mode 

    and here is a beginning of the .cc
    Qt Code:
    1. #include "xmlsqlmap.h"
    2.  
    3. xmlsqlmap["PlayerID"] = "ID";
    4. xmlsqlmap["PlayerName"] = "Name";
    To copy to clipboard, switch view to plain text mode 

    and here a sample of the lovely errors that result.

    Qt Code:
    1. xmlsqlmap.cpp:3: error: ISO C++ forbids declaration of `xmlsqlmap' with no type
    2. xmlsqlmap.cpp:3: error: size of array `xmlsqlmap' has non-integer type
    3. xmlsqlmap.cpp:3: error: conflicting types for `int xmlsqlmap[1]'
    4. xmlsqlmap.h:13: error: previous declaration as `QMap<QString, QString>
    5. xmlsqlmap'
    6. xmlsqlmap.cpp:4: error: ISO C++ forbids declaration of `xmlsqlmap' with no type
    7. xmlsqlmap.cpp:4: error: size of array `xmlsqlmap' has non-integer type
    8. xmlsqlmap.cpp:4: error: redefinition of `int xmlsqlmap[1]'
    To copy to clipboard, switch view to plain text mode 

    can anyone tell my why this happens, and how to fix it ?

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

    Default Re: Problems using QMap

    Quote Originally Posted by importantman View Post
    Qt Code:
    1. #include "xmlsqlmap.h"
    2. xmlsqlmap["PlayerID"] = "ID";
    3. xmlsqlmap["PlayerName"] = "Name";
    To copy to clipboard, switch view to plain text mode 
    This is expression, and you can use expressions only in functions or methods. Outside functions and methods there are only declarations and definitions.

    Quote Originally Posted by importantman View Post

    I am trying to create a QMap as a seperate .h .cpp pair
    You should remember to have definition of the QMap in only one *.cpp file. *.h file should contain declaration.

    This should work:
    Qt Code:
    1. // .h file
    2. #ifndef XMLSQLMAP_H
    3. #define XMLSQLMAP_H
    4.  
    5. #include <qmap.h>
    6. #include <qstring.h>
    7.  
    8. extern QMap<QString,QString> xmlsqlmap; // only declaration
    9. void fill();
    10.  
    11. #endif
    12.  
    13. // .cpp file
    14. #include "xmlsqlmap.h"
    15.  
    16. QMap<QString,QString> xmlsqlmap; // definition
    17.  
    18. void fill() {
    19. xmlsqlmap["PlayerID"] = "ID";
    20. xmlsqlmap["PlayerName"] = "Name";
    21. }
    22.  
    23. // another .cpp file
    24. #include "xmlsqlmap.h"
    25.  
    26. int main() {
    27. fill();
    28. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by importantman View Post

    and here a sample of the lovely errors that result.
    Qt Code:
    1. xmlsqlmap.cpp:3: error: ISO C++ forbids declaration of `xmlsqlmap' with no type
    2. [...]
    To copy to clipboard, switch view to plain text mode 
    Compiler thinks that you are trying to declare/define another variable with name xmlsqlmap.
    Last edited by danadam; 27th October 2006 at 22:48.
    The Wheel weaves as the Wheel wills.

Similar Threads

  1. QMap Problem with arguments.
    By ankurjain in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2006, 12:12
  2. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39
  3. QMap destructor bug
    By Ruud in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2006, 09:08
  4. Is there a Pointer Based QMap or Similar
    By vasudhamirji in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 14:34
  5. Replies: 4
    Last Post: 14th February 2006, 21:35

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.