加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

关于“error:default argument given for parameter...”的解决办法(C++)

(2015-12-14 21:46:15)
标签:

报错

cplusplus

error

解决办法

分类: C_plus_plus
版权说明:未经许可,不得转载。
著作权归博主所有。
本博客一切解释权归博主所有。
编译器报错是因为你的默认参数只能指定一次,但是你进行了多次指定,所以会报错。
以下是示例:
修改前:
class Parallelogram : public Quadrangle
{
private:
double width, height;
public:
Parallelogram(double w = 5, double h = 7, string n = "Parallelogram");
~Parallelogram();
double area();
void draw();
string what();
virtual double Width();
virtual double Height();
};

Parallelogram :: Parallelogram(double w = 5, double h = 7, string n = "Parallelogram")
{
width = w;
height = h;
name = n;
}

此时编译器,就会报错:
[Error] C:\Users\Mac\Documents\C-Free\Temp\未命名1.cpp:31: error: default argument given for parameter 1 of `Parallelogram::Parallelogram(double, double, std::string)'
[Error] C:\Users\Mac\Documents\C-Free\Temp\未命名1.cpp:21: error: after previous specification in `Parallelogram::Parallelogram(double, double, std::string)'

将以上代码多指定的默认参数修改一下,即可成功运行,
修改后的代码:
class Parallelogram : public Quadrangle
{
private:
double width, height;
public:
Parallelogram(double w = 5, double h = 7, string n = "Parallelogram");
~Parallelogram();
double area();
void draw();
string what();
virtual double Width();
virtual double Height();
};

Parallelogram :: Parallelogram(double w, double h, string n)
{
width = w;
height = h;
name = n;
}

//修改后完美运行!

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有