编译错误:error: default argument given for parameter 1 of ‘’ [-fpermissive]

问题:

编译时出现错误提示如下:

u1204@u1204-zhw:~/hwsvn/2sw/4prj_mips/UCP_rt5350/src/trunk$ make
g++ -g3 -Wall -o0 -c ini_file.cpp -o ini_file.o
ini_file.cpp:17:117: error: default argument given for parameter 1 of ‘IniFile::IniFile(const string&, std::string, std::string, std::string)’ [-fpermissive]
ini_file.h:15:14: error: after previous specification in ‘IniFile::IniFile(const string&, std::string, std::string, std::string)’ [-fpermissive]
ini_file.cpp:17:117: error: default argument given for parameter 2 of ‘IniFile::IniFile(const string&, std::string, std::string, std::string)’ [-fpermissive]
ini_file.h:15:14: error: after previous specification in ‘IniFile::IniFile(const string&, std::string, std::string, std::string)’ [-fpermissive]
ini_file.cpp:17:117: error: default argument given for parameter 3 of ‘IniFile::IniFile(const string&, std::string, std::string, std::string)’ [-fpermissive]
ini_file.h:15:14: error: after previous specification in ‘IniFile::IniFile(const string&, std::string, std::string, std::string)’ [-fpermissive]
ini_file.cpp:17:117: error: default argument given for parameter 4 of ‘IniFile::IniFile(const string&, std::string, std::string, std::string)’ [-fpermissive]
ini_file.h:15:14: error: after previous specification in ‘IniFile::IniFile(const string&, std::string, std::string, std::string)’ [-fpermissive]
make: *** [ini_file.o] Error 1
1204@u1204-zhw:~/hwsvn/2sw/4prj_mips/UCP_rt5350/src/trunk$
源程序文件ini_file.cpp:

IniFile::IniFile(const string& filename, const string delim, const string sec_delim_l, const string sec_delim_r)
    : ini_filename_(filename), delim_(delim), sec_delim_l_(sec_delim_l), sec_delim_r_(sec_delim_r), is_dirty_(false), is_saved_(false) {
    Load(ini_filename_);
}
头文件ini_file.h:

class IniFile {
public:
    IniFile(const string& filename = "", const string delim = "=", const string sec_delim_l = "[", const string sec_delim_r = "]");
    ~IniFile();
};
错误在什么地方呢?

解决办法:

1. 原因肯定是由于构造函数使用默认参数造成的。

C++规则,只可能对排列在最后的那些参数提供默认参数,本程序并未违反这一个规则。

查看《C++程序设计语言(特别版)》第202~203页,类Date的声明中构造函数同时声明了缺省参数,而在构造函数的定义中则没有再次声明缺省参数。

参照Date类使用缺省参数的方式,将构造函数定义中的缺省参数声明清除,再次编译,成功通过。

u1204@u1204-zhw:~/hwsvn/2sw/4prj_mips/UCP_rt5350/src/trunk$ make
g++ -g3 -Wall -o0 -c main_ctrl.cpp -o main_ctrl.o
g++ -o local_ctrl main_ctrl.o msgrcv_cmd.o controller.o thread.o ini_file.o dataparse.o msgsnd_data.o sensor_reader.o cJSON.o -L../../drivers -lpthread -lphysicalop -lm
u1204@u1204-zhw:~/hwsvn/2sw/4prj_mips/UCP_rt5350/src/trunk$

问题解决。


进一步的结论:既可以在类的声明中,也可以在函数定义中声明缺省参数,但不能既在类声明中又在函数定义中同时声明缺省参数。

参考地址:http://stackoverflow.com/questions/13295530/this-is-a-new-compile-error-for-me

在编程中,当你看到 "error: default argument missing for parameter 2 of" 这样的错误信息时,通常意味着你在使用具有默认参数值的函数,但是为其中的第二个参数(或其后的某个参数)遗漏了默认值。这种错误可能发生在C++等支持默认参数的编程语言中。 默认参数是指在函数定义时赋予参数的一个默认值。当函数被调用时,如果对应的参数没有提供值,那么就会使用这个默认值。默认参数通常放在函数声明的末尾,以避免混淆。如果一个函数参数有默认值,那么这个参数之后的所有参数都必须有默认值。 例如,在C++中,你可能有以下函数声明: ```cpp void exampleFunction(int a, int b = 10, int c = 20); ``` 在这个例子中,`b` 和 `c` 都有默认值,所以你可以这样调用这个函数: ```cpp exampleFunction(1); // 此时b和c将使用它们的默认值10和20 ``` 但是,如果你这样调用: ```cpp exampleFunction(1, 15); // 此时c会使用默认值20,因为只有b提供了一个值 ``` 上面两种情况都是合法的。但如果函数声明被错误地写成这样: ```cpp void exampleFunction(int a, int b, int c = 20); ``` 此时,如果你只提供了两个参数: ```cpp exampleFunction(1, 15); ``` 编译器就会报错,因为参数 `c` 没有默认值,但又没有被提供一个值。 解决这种错误的方法是为遗漏的参数提供一个默认值,或者在调用函数时提供所有必要的参数值。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值