View Full Version : Mapping structures with logical Address
beemaneni
15th July 2015, 07:50
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
struct {
int a;
float b;
char *a;
}a1;
struct {
int a;
float b;
char *a;
}b1;
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
anda_skoa
15th July 2015, 09:13
i feel we can do with QMap or QHash..
Yes, correct.
QMap<int, struct> map;
the above one could not be carried out.
What's the exact error?
Cheers,
_
beemaneni
15th July 2015, 12:36
It says struct is not allowed to use in the value field as a data type
yeye_olive
15th July 2015, 13:04
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
struct my_struct {
int a;
float b;
char *c;
};
and then you can declare
QMap<int, my_struct> map;
d_stranz
15th July 2015, 22:42
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).
beemaneni
16th July 2015, 10:20
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
yeye_olive
16th July 2015, 11:03
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>.
anda_skoa
16th July 2015, 11:18
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,
_
d_stranz
16th July 2015, 16:20
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 (http://doc.qt.io/qt-5/properties.html).
And no, I'm not going to write a working example for you. The documentation is clear.
beemaneni
17th July 2015, 06:51
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
jefftee
17th July 2015, 08:43
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:
// int is the key to the map and is the structure identifier/type, i.e. 1001, 1002, 1003, 1004, etc.
// void* is a pointer to the structure type, i.e. user_input*, batt_status*, etc.
QMap<int, void*> struct_map;
struct_map.insert(1001, new user_input);
struct_map.insert(1004, new batt_status);
Then, to get the structure pointer out of the QMap, you cast the void* back to the correct structure pointer:
if (struct_map.contains(1001))
{
user_input *ui = static_cast<user_input*>(struct_map.value(1001));
// process user_input structure here
}
if (struct_map.contains(1004))
batt_status *bs = static_cast<batt_status*>(struct_map.value(1004));
// process batt_status here
}
Hope that helps.
anda_skoa
17th July 2015, 10:26
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,
_
jefftee
17th July 2015, 20:06
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.