简体   繁体   中英

How do pass a class member (variable?) to a function?

Disclaimer: This is for an assignment, but I'm not looking for you guys to write my code. I'm looking for advice on what I'm doing wrong and a point in the right direction.

There is a file with formatted data (time, temperature, wind speed). I am supposed to create a linked list with three different "chains"; each chain has the same data but is differing in the order such that the chain with time will read all the data in time-sequential order, the chain with temp will read the data in temp-sequential order, and the chain with wind speed will read the data in wind speed-sequential order.

I've written a function that can successfully read in the data from the given pointer and organize the list correctly by time. But now I'm looking to use that same function to read in the same pointer and organize the other 2 lists.

Here is the relevant code:

void datalogger::addData(int timestamp, double temperature, double windspeed){

weatherdata *n;
n = new weatherdata;

n->time = timestamp;
n->temp = temperature;
n->wind = windspeed;
n->next = nullptr;

ll.addData(n, 1);
}

void linkedlist::addData(weatherdata *newData, int selectionNo){

weatherdata *selection;

switch(selectionNo){
    //linkedlist: TIME
    case 1:
        selection = timeHead;
        break;
    //linkedList: TEMP
    case 2:
        selection = tempHead;
        break;
    //linkedList: WIND
    case 3:
        selection = windHead;
        break;
}


int marker = 0;
weatherdata *node;
weatherdata *current;
weatherdata *nextNumber;

node = new weatherdata;
node = newData;
node->next = nullptr;

//very first entry
if (selection == nullptr){
    selection = node;
    marker = 1;
}

//item should instead be in the first position
if (newData->time < selection->time){
    nextNumber = timeHead;
    selection = node;
    node->next = nextNumber;
}

else {
    //traverse the list until we find the correct place to put it in
    current = selection;
    nextNumber = selection->next;

    //while "not yet at the end of the list"
    while (nextNumber != nullptr){

        //if its the same as another item DELETE
        if ((newData->time == current->time) || (newData->time == nextNumber->time)) {
            marker = 1;
            break;
        }

        //the new item goes between the current and nextNumber
        else if ((current->time < newData->time) && (newData->time < nextNumber->time)){
            current->next = node;
            node->next = nextNumber;          
            break;
        }

        //otherwise increment current and nextNumber for the next comparison
        else {
            current = nextNumber;
            nextNumber = nextNumber->next;
        }
    }   

    //item goes at the end of the list
    if ((nextNumber == nullptr) && (marker != 1)){
        current->next = node;
    }           
}

switch(selectionNo){
    case 1:
        timeHead = selection;
        break;
    case 2:
        tempHead = selection;
        break;
    case 3:
        windHead = selection;
        break;
}
}

Basically what I want to do is turn this:

timeHead->time

into this:

whicheverLinkedListHeadIwant->whicheverClassMemberIwant

That way, I can use the same function to process all three linked lists. As you might have noticed, using a switch statement at the beginning of my linkedlist::addData function I was able to get the code to process "whicheverLinkedListHeadIwant" but for the life of me I can't manage to get the "whicheverClassMemberIwant" to work.

This is what I've tried:

char* classMemberSelection = "temp"; or char* classMemberSelection = "wind"

whicheverLinkedListHeadIwant->classMemberSelection;

But the compiler just returns an error saying that classMemberSelection is not a member. I'm at a loss for how to make the second part whichever I want it to be.

I'm sorry if this question does not make sense. I tried my best to be as clear as possible but I am still a beginner with all of this. If you need me to post additional pieces of the project then I'd be happy to oblige.

First we define three functions, one for each type of comparison

bool comparetime(weatherdata * left, 
                 weatherdata * right)
{
    return left->time < right->time;
}
bool comparetemp(weatherdata * left, 
                 weatherdata * right)
{
    return left->temp < right->temp;
}
bool comparewind(weatherdata * left, 
                 weatherdata * right)
{
    return left->wind < right->wind;
}

All these guys do is tell you if one is smaller than the other. Now we tweak

void linkedlist::addData(weatherdata *newData, int selectionNo)

to

void linkedlist::addData(weatherdata *newData, 
                         bool (*comparefp)(weatherdata * left, 
                                           weatherdata * right))

to take one of the above functions as a parameter. Next we want to find where in the list newData goes and pop it in:

while (nextNumber != nullptr && comparefp(newData, nextNumber))
// keep looking until end of list or we pass the insert location 
{
    lastNumber = nextNumber;
    nextNumber = nextNumber.next;
}
// insert into list
newData.next=nextNumber;
lastNumber.next=newData;

and calling linkedlist::addData

void datalogger::addData(int timestamp, double temperature, double windspeed){

    weatherdata *n;
    n = new weatherdata;

    n->time = timestamp;
    n->temp = temperature;
    n->wind = windspeed;
    n->next = nullptr;

    // add to each list    
    timelist.addData(n, comparetime);
    templist.addData(n, comparetemp);
    windlist.addData(n, comparewind);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM