简体   繁体   中英

Passing structs to a void function

My question is how do you pass a struct.variable (or the struct array) to the void function. Basically the code looks as follows:

Structs

struct Person{
    string surname;
    string BType;
    string organ;
    int age;
    int year, ID, IDp;
} Patient[50], Donor[50];

int i; // counter variables for the arrays such as Patient[i].BType... etc
int i1; 

Then the code for the function is a line like this:

void compare(int &i, int &i1, Person &Patient[50], Person &Donor[50]);

I tried to pass the i , i1 , Patient and Donor structs. Why won't this work? Is there a special way to pass these sorts of structs to a function?

The values into the variable structs also are read from a file (don't think that changes anything here). Any ideas?

Your function prototype is incorrect. To pass a fixed array type reference, you must qualify the reference part of the parameter outside the array index declaration.

void compare(int &i, int &i1, Person (&Patient)[50], Person (&Donor)[50])
//  note parens ----------------------^-------^-------------^------^

Invoke as simply

compare(i, i1, Patient, Donor);

It is interesting to note you can do this with a template that guarantees the fixed-array size via deduction.

template<size_t N>
void compare(int &i, int &i1, Person (&Patient)[N], Person (&Donor)[N])
{
    // N is guarenteed to be the size of your array. You can use it
    //  as you would 50 in your code.
    for (size_t i=0; i<N;++i)
    {
        // do something with Patient and Donor elements
    }
}

This has the added benefit of allowing instantiating with different array sizes. Ie You can also do this:

Person Patient[50], Donor[50];
Person MorePatients[10], MoreDonors[10];

....

compare(i, i1, Patient, Donor);
compare(i, i1, MorePatients, MoreDonors)

and it will compile correctly. I suggest you experiment with it, you may find it useful.

In addition to what WhozCraig has said, you could even put variadic template parameters in there to allow passing any number of types of things you can find in a hospital, such as nurses and midwives!

template <typename T1, size_t N1>
void compare(int &i, int &i1, T1 (&array)[N1]) {
    for (const auto& elem : array) {
        std::cout << elem.name << std::endl;   
    }
}

template <typename T1, size_t N1, typename... Ts, size_t... Ns>
void compare(int &i, int &i1, T1 (&array)[N1], Ts (&... arrays)[Ns]) {
    for (const auto& elem : array) {
        std::cout << elem.name << std::endl;   
    } 
    std::cout << std::endl;
    compare(i, i1, arrays...);
}

struct Nurse
{ std::string name; };

struct Midwife
{ std::string name; };


int main() {
    Nurse nurses[] = {{"Nina"}, {"Austin"}};
    Midwife midwives[] = {{"Matriona"}, {"Lizbeth"}, {"Jill"}};
    Nurse evenMoreNurses[] = {{"Maria"}, {"Nick"}, {"Martine"}, {"Ashley"}};

    int i{};
    int i1{};

    compare(i, i1, nurses, midwives, evenMoreNurses);
}

Do you realize now just how fun C++ can be?

just declare function like this:

void compare(int i, int i1, Person *Patient, Person *Donor);

you can invoke like:

compare(i, i1, Patient, Donor)

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