提问者:小点点

如何将结构传递到功能?


下面是我要做的:

#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;
}

我想做一个函数,从一个结构打印数据。


共1个答案

匿名用户

将函数声明移动到结构后面。

 #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;
}