使用引用[duplicate]将结构传递给函数时出错

问题描述 投票:-3回答:1

试图将结构矩形传递到功能区域以通过引用调用进行计算

#include <iostream>
using namespace std;

int area(struct rectangle & r1)
{
    return r1.length*r1.width;
}

struct rectangle
{
    int length;
    int width;
};

int main()
{
    struct rectangle r={10,5};
    int total=area(r);
    std::cout << "Area is:" << total <<  std::endl;

}
c++ structure pass-by-reference
1个回答
1
投票

您的代码顺序错误,应该看起来像这样

struct Rectangle
{
    ...
};

int area(Rectange& r1)
{
    ...
}

您必须在第一次使用Rectangle之前定义它,而不是在以后定义。

© www.soinside.com 2019 - 2024. All rights reserved.