如何初始化作为具有默认值的类的函数参数 [英] how to initialize function arguments that are classes with default value

查看:297
本文介绍了如何初始化作为具有默认值的类的函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux gcc环境中工作,我需要初始化作为默认值类的函数参数。
当我使用类的临时实例时,会产生如下错误:[argument argument]的默认参数具有类型[class name]。
例如:

I'm working on Linux gcc environment and I need to initilize function arguments that are classes with default values. When I do that with temporary instance of the class it makes an error like this: "default argument for [function argument] has type [class name]. for example:

void foo(std::wstring& str = std::wstring())

错误:'std :: wstring& str'的默认参数类型为'std :: wstring'
PS这个代码编译时没有任何错误或警告VC ++

error: default argument for 'std::wstring& str' has type 'std::wstring' P.S. this code is compiled without any error or warning with VC++.

如何初始化默认值?

推荐答案

你应该在一个非const引用绑定一个右值。说 std :: wstring const& str ,它应该工作。

This is supposed to not compile. You are trying to bind an rvalue to a non-const reference. Say std::wstring const & str and it should work.

这篇关于如何初始化作为具有默认值的类的函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆