今天在尝试使用默认参数时,在linux的g++编译下却出现了如下报错:
test.cpp: In function ‘int mores(int, int)’: test.cpp:16:24: error: default argument given for parameter 2 of ‘int mores(int, int)’ [-fpermissive] int mores(int m,int s=1) ^ test.cpp:8:5: note: previous specification in ‘int mores(int, int)’ here int mores(int m,int s=1); ^ shell returned 1
代码如下:
#include <iostream>
int mores(int m,int s=1);
using namespace std;
int main ()
{
int val=4;
cout<<mores(val);
return 0;
}
int mores(int m,int s=1)
{
return m+s;
}
百思苦想却不得错在何处,后来回头仔细去看书发现《C++ primer》上有这么一句话:
请注意,只有原型指定了默认值,函数定义与没有默认参数时完全相同;
于是把函数定义部分稍作修改:
int mores(int m,int s)
而后,问题解决!
576




被折叠的 条评论
为什么被折叠?



