C++函数默认参数(转)
在代码中使用到了函数的默认参数,在函数的定义和实现中都填写的默认参数,结果出现了错误:

代码:
#ifndef FIRSTPAGE_H
#define FIRSTPAGE_H #include <QWizardPage>
#include "ui_firstdialog.h" class FirstPage : public Ui::FirstDialog, public QWizardPage
{
public:
FirstPage(QWidget *parent = );
}; #endif // FIRSTPAGE_H
#include "fifthpage.h" FifthPage::FifthPage(QWidget *parent = ) :
QWizardPage(parent)
{
}
当去掉了实现文件中的默认参数值时,通过了编译,于是就考虑是不是因为同时在定义文件中和实现文件中都填写了默认参数造成了这个错误。在网上搜到一篇讲的比较详细的文章: 函数声明和函数定义中的默认参数浅析
默认参数是存在于函数的声明中,还是函数的定义中呢?
- #include <iostream>
- #include <tchar.h>
- using namespace std;
- void SetHeight(double dHeight = 183.5);
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- SetHeight(181.5);
- return 1;
- }
- void SetHeight(double dHeight = 183.5)
- {
- cout << _T("身高为:") << dHeight << endl;
- }
- #include <iostream>
- #include <tchar.h>
- using namespace std;
- void SetHeight(double dHeight = 183.5);
- void SetHeight(double dHeight = 183.5)
- {
- cout << _T("身高为:") << dHeight << endl;
- }
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- SetHeight(181.5);
- return 1;
- }
- #include <iostream>
- #include <tchar.h>
- using namespace std;
- void SetHeight(double dHeight = 183.5);
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- SetHeight();
- return 1;
- }
- void SetHeight(double dHeight)
- {
- cout << _T("身高为:") << dHeight << endl;
- }
- #include <iostream>
- #include <tchar.h>
- using namespace std;
- void SetHeight(double dHeight);
- void SetHeight(double dHeight = 183.5)
- {
- cout << _T("身高为:") << dHeight << endl;
- }
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- SetHeight();
- return 1;
- }
- #include <iostream>
- #include <tchar.h>
- using namespace std;
- void SetHeight(double dHeight = 183.5);
- void SetHeight(double dHeight)
- {
- cout << _T("身高为:") << dHeight << endl;
- }
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- SetHeight();
- return 1;
- }
- #include <iostream>
- #include <tchar.h>
- using namespace std;
- void SetHeight(double dHeight);
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- SetHeight();
- return 1;
- }
- void SetHeight(double dHeight = 183.5)
- {
- cout << _T("身高为:") << dHeight << endl;
- }
- // Head.h
- #pragma once
- #include <tchar.h>
- #include <iostream>
- using namespace std;
- void SetHeight(double dHeight = 183.5);
- //Body.cpp
- #include "Head.h"
- void SetHeight(double dHeight)
- {
- cout << _T("身高为:") << dHeight << endl;
- }
- //Main.cpp
- #include "Head.h"
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- SetHeight();
- return 1;
- }
- // Head.h
- #pragma once
- #include <tchar.h>
- #include <iostream>
- using namespace std;
- void SetHeight(double dHeight);
- //Body.cpp
- #include "Head.h"
- void SetHeight(double dHeight = 183.5)
- {
- cout << _T("身高为:") << dHeight << endl;
- }
- //Main.cpp
- #include "Head.h"
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- SetHeight();
- return 1;
- }
- // Head.h
- #pragma once
- #include <tchar.h>
- #include <iostream>
- using namespace std;
- void SetHeight(double dHeight);
- //Body.cpp
- #include "Head.h"
- void SetHeight(double dHeight = 183.5)
- {
- cout << _T("身高为:") << dHeight << endl;
- }
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- SetHeight();
- return 1;
- }
C++函数默认参数(转)的更多相关文章
- C++函数重载遇到了函数默认参数情况
一.C++中的函数重载 什么是函数重载? 我的理解是: (1)用一个函数名定义不同的函数: (2)函数名和不同参数搭配时函数会有不同的含义: 举例说明: #include <stdio.h> ...
- 如何在ES5与ES6环境下处理函数默认参数
函数默认值是一个很提高鲁棒性的东西(就是让程序更健壮)MDN关于函数默认参数的描述:函数默认参数允许在没有值或undefined被传入时使用默认形参. ES5 使用逻辑或||来实现 众所周知,在ES5 ...
- 【转】Python函数默认参数陷阱
[转]Python函数默认参数陷阱 阅读目录 可变对象与不可变对象 函数默认参数陷阱 默认参数原理 避免 修饰器方法 扩展 参考 请看如下一段程序: def extend_list(v, li=[]) ...
- Python面试题目之Python函数默认参数陷阱
请看如下一段程序: def extend_list(v, li=[]): li.append(v) return li list1 = extend_list(10) list2 = extend_l ...
- Python进阶-函数默认参数
Python进阶-函数默认参数 写在前面 如非特别说明,下文均基于Python3 一.默认参数 python为了简化函数的调用,提供了默认参数机制: def pow(x, n = 2): r = 1 ...
- ES6函数默认参数(Default Parameters)
语言更新时每一个新增的特性都是从千百万开发者需求里提取过来的,规范采用后能减少程序员的痛苦,带来便捷. 我们经常会这么写 function calc(x, y) { x = x || 0; y = y ...
- 【matlab】设定函数默认参数
C++/java/python系列的语言,函数可以有默认值,通常类似如下的形式: funtion_name (param1, param2=default_value, ...) 到了matlab下发 ...
- ES6新特性(函数默认参数,箭头函数)
ES6新特性之 函数参数的默认值写法 和 箭头函数. 1.函数参数的默认值 ES5中不能直接为函数的参数指定默认值,只能通过以下的变通方式: 从上面的代码可以看出存在一个问题,当传入的参数为0或者 ...
- C++函数默认参数
C++中允许为函数提供默认参数,又名缺省参数. 使用默认参数时的注意事项: ① 有函数声明(原型)时,默认参数可以放在函数声明或者定义中,但只能放在二者之一 double sqrt(double f ...
- 3.C++内联函数,默认参数,占位参数
本章主要内容: 1)内联函数(替代宏代码段) 2)默认参数 3)占位参数 1.C++的内联函数分析 1.1讲解内联函数之前,首先回忆下之前讲的define宏定义: 之前讲过宏定义会经过预处理器进行文本 ...
随机推荐
- 集成禅道和svn
转载:http://www.zentao.net/book/zentaopmshelp/137.html 说明:svn集成功能配置会比较复杂,我们会尽量通过文档来帮助大家配置成功!如果实在配置不成功的 ...
- http://jingyan.baidu.com/article/7f41ecec1b7a2e593d095ce6.html
http://jingyan.baidu.com/article/7f41ecec1b7a2e593d095ce6.html http://www.linuxeden.com/html/softuse ...
- JAVA , TOMCAT , AXIS2 环境变量配置
想要成功配置Java的环境变量,那肯定就要安装JDK,才能开始配置的. 安装JDK 向导进行相关参数设置.如图: 正在安装程序的相关功能,如图: 选择安装的路径,可以自定义,也可以默认路径.如图: 成 ...
- laravel5.1安装
Laravel 于6月9日正式发布了 5.1 最新 LTS 版本.这是 Laravel 历史上第一个提供 LTS(长期支持 - long-time support) 支持的版本. 首先使用Larave ...
- struts2-core-2.0.14更新到2.3.15
struts2-core-2.0.14更新到2.3.15 将低版本的struts2-core更新到最新版本2.3.15,更新jar包,有这个几个 1. struts2-core-2.0.14.jar ...
- [React] Safely setState on a Mounted React Component through the useEffect Hook
In the class version of this component, we had a method called safeSetState which would check whethe ...
- STL - C++ 11的Lambda表达式(上)
Lambda始自C++ 11,是一种在表达式或语句内指定函数行为的定义式. 你可以定义函数行为作为对象,以inline实参的形式传给算法作为predicate(判断式). eg: std:transf ...
- Android Studio优秀插件汇总
- 深入理解javascript闭包【整理】
原文链接:http://www.cn-cuckoo.com/2007/08/01/understand-javascript-closures-72.html 英文原文:http://www.jibb ...
- DBCP( 二) DataBase Connection Pool 的使用
使用DBCP必须用的三个包: commons-dbcp-1.2.1.jar, commons-pool-1.2.jar, commons-collections-3.1.jar. 配置参数. Java ...