简体   繁体   English

将结构传递给void函数

[英]Passing structs to a void function

My question is how do you pass a struct.variable (or the struct array) to the void function. 我的问题是如何将struct.variable(或struct数组)传递给void函数。 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. 我试图通过ii1PatientDonor结构。 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! 除了WhozCraig所说的以外,您甚至可以在其中放置可变参数模板参数,以传递在医院中可以找到的各种类型的事物,例如护士和助产士!

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? 您现在意识到C ++会多么有趣吗?

just declare function like this: 只是这样声明函数:

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

you can invoke like: 您可以像这样调用:

compare(i, i1, Patient, Donor) 比较(i,i1,患者,供体)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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