C++ 使用参数的默认值

标签 c++ parameters default-value

我是 C++ 的新手(来自 C# 背景),我有一个具有以下签名的函数

 void AddBenchNode(ref_ptr<Group> root ,ref_ptr<Node> benches, bool setAttitude = false, float scale_x =.15, float scale_y =15, float scale_z = 15, int positionx = 250, int positiony = 100, int positionz =0 )
{

}

但是当我尝试调用下面的代码时,我得到一个错误,提示函数不接受 4 个参数。

//then I try to call my function like so
AddBenchNode(root, benches, false, 250);

但我却收到以下错误消息

错误 C2660:“AddBenchNode”:函数不接受 3 个参数

希望解释一下 C++ 是如何做到这一点的?

最佳答案

检查 .hpp 文件中的原型(prototype)。它可能被声明为

void AddBenchNode(ref_ptr<Group> root ,ref_ptr<Node> benches, bool setAttitude, 
                  float scale_x, float scale_y, float scale_z, int positionx, 
                  int positiony, int positionz);

编辑:标题中的原型(prototype)应该是

void AddBenchNode(ref_ptr<Group> root ,ref_ptr<Node> benches, bool setAttitude = false, float scale_x =.15, float scale_y =15, float scale_z = 15, int positionx = 250, int positiony = 100, int positionz =0 );

然后你的cpp文件应该只有

void AddBenchNode(ref_ptr<Group> root ,ref_ptr<Node> benches, bool setAttitude, float scale_x, float scale_y, float scale_z, int positionx, int positiony, int positionz)
{

}

也就是说,默认参数在原型(prototype)中,而不是在实际定义中。

关于C++ 使用参数的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11346135/

相关文章:

c++ - 如何安装系统范围的 nuget 包?

javascript - 如何使用 onclick 调用带参数的函数?

ruby - 如何确定是否在 Ruby 中传递了可选参数

solr数据导入默认值

c++ - 如何在 C++ 中模拟 5 级流水线?

c++ - MySQL C API : How to work with row from mysql_fetch_row()?

c++ - 捕获 QSpinBox 文本字段 mouseEvents

python - 检查传递的参数在 Python 中是否有效的最佳方法是什么?

compiler-errors - VHDL - 分配默认值

mysql - Liquibase 添加默认值