繁体   English   中英

Linux中结构参考构建的默认参数失败

[英]default argument for structure reference build failed in linux

好吧,假设有一个功能

void fun(const MyStructure& argu1 = MyStructure(), MyStructure& argu2 = Mystructure())

argu2不是const,因为我想在函数中修改其值。

调用函数:

MyStructure a; 
MyStructure b; 
fun(a,b);

构建在Windows中成功,但在Linux中失败,错误是

default argument for parameter of type 'MyStructure&' has type 'MyStructure'

但是,如果我删除了非常量的第二个默认参数,则构建将在Windows和Linux中均成功。有人可以告诉我为什么以及如何解决它吗?

您可以使用重载来手动处理可选的第二个非常量引用参数:

void fun( MyStruct const& arg1, MyStruct& arg2)
{
    // do the real work
}

void fun( MyStruct const& arg1 = MyStruct())
{
    MyStruct arg2;  // a dummy argument that can be changed, but we'll
                    //  throw those changes away
    fun( arg1, arg2);
}

暂无
暂无

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

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