简体   繁体   English

如何将结构传递给 function?

[英]how to pass structure to function?

here is what i am trying to do:这是我想要做的:

#include <iostream>

void out(box boxx);
struct box {
    char charr;
    float floatt;
};

int main()
{
    box boxx;
    boxx.charr = 'f';
    boxx.floatt = 2.5;
    out(boxx);
}

void out(box boxx)
{
    std::cout << boxx.charr << "\n" << boxx.floatt;
}

I want to make a function that prints data from a structure.我想制作一个从结构中打印数据的 function。

Move function declaration to after struct.将 function 声明移到结构之后。

 #include <iostream>

 
 struct box {
 char charr;
 float floatt;
 };
void out(box boxx);
int main()
{
  box boxx;
 boxx.charr = 'f';
 boxx.floatt = 2.5;
 out(boxx);
}

void out(box boxx)
{
 std::cout << boxx.charr << "\n" << boxx.floatt;
}

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

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