Results 1 to 13 of 13

Thread: Mapping structures with logical Address

  1. #1
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Mapping structures with logical Address

    Hi
    I need to map few structures with the integer.I could not know how to do it?Can anyone help me
    Here is the example like:
    i have few structures like this
    Qt Code:
    1. struct {
    2. int a;
    3. float b;
    4. char *a;
    5. }a1;
    6. struct {
    7. int a;
    8. float b;
    9. char *a;
    10. }b1;
    To copy to clipboard, switch view to plain text mode 

    i want to map these structures with some logical address say 1001,1002 etc;;

    i feel we can do with QMap or QHash..

    QMap<int, struct> map;
    the above one could not be carried out.

    i want to insert all that structures with logical address.
    can anyone suggest some solution?


    Thanks
    Bala Beemaneni
    Last edited by anda_skoa; 15th July 2015 at 09:14. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mapping structures with logical Address

    Quote Originally Posted by beemaneni View Post
    i feel we can do with QMap or QHash..
    Yes, correct.

    Quote Originally Posted by beemaneni View Post
    QMap<int, struct> map;
    the above one could not be carried out.
    What's the exact error?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Mapping structures with logical Address

    It says struct is not allowed to use in the value field as a data type

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mapping structures with logical Address

    Quote Originally Posted by beemaneni View Post
    It says struct is not allowed to use in the value field as a data type
    Of course: struct is a C++ keyword, not a type identifier. Just give a name to your type, as in
    Qt Code:
    1. struct my_struct {
    2. int a;
    3. float b;
    4. char *c;
    5. };
    To copy to clipboard, switch view to plain text mode 
    and then you can declare
    Qt Code:
    1. QMap<int, my_struct> map;
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Mapping structures with logical Address

    But you cannot use the same QMap to store different types of structs - the structs you show in your first example are different as far as C++ is concerned, because they are each declared individually even though they contain the same data types in the same order. And by the way, you can't have a struct that contains the same member name twice (int a, char * a).

  6. #6
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Mapping structures with logical Address

    i declared names of variables just for example.i got what you guys said....but my requirement is like
    i have 100 variables that has to be mapped to some logical address so that when a variable has to be set or get i get request by logical address.
    Then i need to find to which variable that is mapped and then update the variable.Can anyone suggest?





    Regards
    Bala Beemaneni

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mapping structures with logical Address

    Please be specific about what you are trying to achieve. What input does your algorithm receive? What output does it produce?

    From what you wrote I understand that the input is a string (variable name), and the output is an integer (logical address). You can map QStrings to ints with a QMap<QString, int>.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mapping structures with logical Address

    Quote Originally Posted by beemaneni View Post
    i declared names of variables just for example.
    Your error is not related to variable names but type names.
    A template, in this case QMap, needs type names.

    See comment #4

    Cheers,
    _

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Mapping structures with logical Address

    i have 100 variables that has to be mapped to some logical address
    If I understand you correctly, you have a variable named "a" and at run time, someone will type "a" and the new value to set for "a", and you want a way to execute code that does something like this:

    setValueOf( "a" ) = newValueOfA;

    Is that what you want to do? If so, you might be able to do it using Qt's Property System.

    And no, I'm not going to write a working example for you. The documentation is clear.

  10. #10
    Join Date
    Jul 2013
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Mapping structures with logical Address

    Hi guyz
    Let me brief my requirement

    Consider i have few structures as follows:
    struct user_input
    {
    int crc;
    int userdata;
    int dummy;
    }uinfo;
    struct batt_status
    {
    int bat_crc;
    int bat_vol;
    int status;
    }batinfo;

    like that i have n number of structures..
    Here what i need to do is from 1st structure i need to map crc,userdata,dummy with 1001,1002,1003 respectively and from next structure bat_crc,bat_vol,status as 1004,1005,1006 respectively and so on.So i get data via serial port requesting for above structures with logical address.What i need is i need to map all the structures data with only one QMap.
    For Ex:
    when 1001 is received i need to find out using map and then send back the values to end user.

    Hope my requirement this time is clear.

    Regards
    Bala Beemaneni

  11. #11
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Mapping structures with logical Address

    Do all of your structures consist solely of three integer values and the first value is always the "identifier" for the structure type? If so, you can simplify and use a single structure, which you can store in the QMap or more likely, store a pointer to the structure in the QMap.

    If you really have a need to define a bunch of different structure types in the QMap, you can use a void pointer, such as:

    Qt Code:
    1. // int is the key to the map and is the structure identifier/type, i.e. 1001, 1002, 1003, 1004, etc.
    2. // void* is a pointer to the structure type, i.e. user_input*, batt_status*, etc.
    3. QMap<int, void*> struct_map;
    4. struct_map.insert(1001, new user_input);
    5. struct_map.insert(1004, new batt_status);
    To copy to clipboard, switch view to plain text mode 

    Then, to get the structure pointer out of the QMap, you cast the void* back to the correct structure pointer:

    Qt Code:
    1. if (struct_map.contains(1001))
    2. {
    3. user_input *ui = static_cast<user_input*>(struct_map.value(1001));
    4. // process user_input structure here
    5. }
    6.  
    7. if (struct_map.contains(1004))
    8. batt_status *bs = static_cast<batt_status*>(struct_map.value(1004));
    9. // process batt_status here
    10. }
    To copy to clipboard, switch view to plain text mode 


    Hope that helps.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mapping structures with logical Address

    Statically casting based on assumption of index sounds very fragile.

    The suggestion to use a single struct type if they all have the same data fields by type sound much better.

    If they do not and the example was just indicentally having the same fields, I would suggest looking into a polymorphic approach.
    E.g. having an interface that specifies a method to parse received data and then implement that appropriately in each data type.

    Cheers,
    _

  13. #13
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Mapping structures with logical Address

    Quote Originally Posted by anda_skoa View Post
    Statically casting based on assumption of index sounds very fragile.

    The suggestion to use a single struct type if they all have the same data fields by type sound much better.
    @anda_skoa, I would agree with that. I was only trying to offer a way to accomplish what the OP is trying to do if he requires many different structures.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. little q: illogical logical more c++
    By T1001 in forum Qt Programming
    Replies: 16
    Last Post: 13th July 2015, 17:29
  2. Suggestions for logical expression syntax?
    By rakkar in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2009, 20:24
  3. Non logical Errors - qmake
    By ahmhdy in forum Qt Programming
    Replies: 7
    Last Post: 19th June 2009, 21:11
  4. little logical question........
    By sudheer in forum Qt Tools
    Replies: 3
    Last Post: 8th May 2008, 14:15
  5. Logical fonts?
    By sdfisher in forum Qt Programming
    Replies: 1
    Last Post: 1st March 2007, 13:01

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.