Okay, I was wondering if there was an easy way to make a function that searches an array of objects. Here's what I was thinking:
1. You pass the array of objects to the function along with the thing you're searching for and the field you want to find it in.
2. It displays all the data for all the objects that have what you were looking for
i.e. you have a player structure with 3 strings: name, favorite color, and position.
The function can search and display all the players in the array for "red" being in the favorite color section, or "center" being their position.
I've got most of it figured out, I just didn't know how to tell the function which field to search. Any clue?
Minime80
11-11-2000, 11:26 PM
Is this even possible?
fafnir
11-12-2000, 08:23 PM
I am assuming that your array contains all the same class and that you know what it is.
If this is the case then I think something this might be what you want (I haven't compiled, but it should illustrate what I mean):
//class to be used in the array
class Character {
private:
char **name;
char **colour
public:
Character(const char **charName, const char **charColour) {
strcpy(name, charName);
strcpy(colour, charColour);
}
class CharacterArray {
private:
Character **array;
int length;
public
CharacterArray() : length(0) {}
//search takes a pointer to a Character
//method that returns a char** and takes
//no parameters, and a char** to compare
//the result to, and returns the Character
//object that matches.
Character *search(
char **(Character::*comparisonMethod)(),
const char **comparisonValue) {
for (int i = 0; i < length; i++) {
if (strcmp(array[i]->*comparisonMethod(),
comparisonValue) == 0)
return array[i];
}
}
//... methods to populate array etc
};
int main(void) {
CharacterArray characterArray;
//... array is initialised at some point
Character *fred = characterArray.search(
&Character::getName(), "fred");
Character *red = characterArray.search(
&Character::getColour(), "red");
}
The key is the declaration: char **(Character::*comparisonMethod)()
this is a pointer called comparisonMethod, that points to a method in a Character object that returns char** and takes no parameters. This allows you to pass any method from the Character class that matches this signiature.
HTH,
Fafnir
[This message has been edited by fafnir (edited 12 November 2000).]
[This message has been edited by fafnir (edited 12 November 2000).]
Minime80
11-13-2000, 01:38 AM
Okay, I sort of get it. So how would I do it with a structure something like this:
struct player
{
char name[10];
char favorite_color[10];
char position[10];
};
void printp(char* s, struct player** p, int l, int field)
{
int i;
for (i=0;i<l;++i)
{
switch (field)
{
case 1:
if (strcmp((const char*)s,((const char*)p[i]->name))==0) do_printp(p[i]);
break;
case 2:
if (strcmp((const char*)s,((const char*)p[i]->favorite_color))==0) do_printp(p[i]);
break;
case 3:
if (strcmp((const char*)s,((const char*)p[i]->position))==0) do_printp(p[i]);
break;
default:
{
}
};
}
}
int main()
{
int errcode=init(array,ARRAY_LEN);
switch (errcode)
{
case -1:
reclaim(array,ARRAY_LEN);
break;
case 0:
{
I probably left something out, oh well. http://www.linuxnewbie.org/ubb/smile.gif
Minime80
11-13-2000, 09:21 PM
Right... I think I should regroup and try to explain what I want to do a different way, or maybe just be more specific. I don't even know if it's possible, but after messing with some of the code here I got it down to 3 errors. Anyways, here's what I'm trying to say. I want to have an array of these player structures. Not another object holding this array though. I just want to declare it in main like this:
player players[5];
Then I want a function in the program (not in the structure, functions for returning the data are fine in the structure) where I can send it the array that I just created, somehow tell it what field to look in (without using if's if possible because I don't want 100 if's when there's 100 different data members in the structure), and also send it the string I'm looking for. Now, inside this search function there should just be one loop with one if statement containing something that accomplishes this:
strcmp(value, player[i].field)
where value is the string I'm looking for and field is the data member of the player that I want to find it in. After that, if they match it will print out that players' data (that's the easy part), and then keep going through the loop until it gets to the end of the array.
Well, I hope this clears things up a little. I have learned a ton from what you guys have posted already, it just wasn't exactly what I was asking, but it was helpful. Thanks for the help so far... keep it coming... I'm learning a LOT! http://www.linuxnewbie.org/ubb/smile.gif
Minime80
11-13-2000, 09:29 PM
Oh, about those errors I'm getting. I made a function with a *comparisonMethod() and two of the errors are when I call the function (I called it twice in my program). It says that it can't execute the function without an object, but I don't want to execute the function until later when I'm in the loop and start comparing the results of the function it's pointing to in the structure to what I'm looking for. The other error is in the actual function itself, and it's saying this: "no matching function for call to 'player::comparisonMethod()'"
I'm not exactly sure what it's trying to tell me here. Anyways, that's what I've got so far.
The_Stack
11-14-2000, 12:19 AM
Haven't you looked at my code!?
Minime80
11-14-2000, 02:50 AM
Originally posted by The_Stack:
Haven't you looked at my code!?
Oh, sorry. I guess I didn't really look at it closely enough. Sorry. I think it's just because I never really did C. I just learned C++ and those malloc's and stuff threw me. Speaking of those, does it have to be dynamically created. It's just more involved than I need to get right now. I know that there's only going to be 5 players, so I just need to make that array of 5 players. I've got a function to get the info from the user. Well, here, I'll post what I've been trying to do. I know it doesn't work, but maybe one of you could change it a tad and show me what I need to do to get it to work. #include <iostream.h>
#include <string.h>
void search(player *person, char **(player::*comparisonMethod)(), const char *value)
{
cout << "\n All players whose data contains " << value << ":";
cout << "\nNAME " << "FAVORITE COLOR " << "POSITION ";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
for (short i=0;i<5;++i)
{
if (!strcmp(value,person[i].comparisonMethod()))
{
cout.width(17);
cout << person[i].name;
cout.width(21);
cout << person[i].color;
cout << person[i].position << endl;
}
}
}
There ya have it. Probably not the best of work, but I've only been doing it about 5 months now. Sorry again that I didn't pay more attention to what you had done there Stack.
[This message has been edited by Minime80 (edited 14 November 2000).]
justlinux.com
Copyright Internet.com Inc. All Rights Reserved.