错误代码:
#include <iostream>
using namespace std;
class vehicle
{
protected:
double speed; //速度,公里/小时
int wheels; //轮子数
double weight; //重量
public:
vehicle(double speed = 80, int wheels = 4, double weight = 1000);
virtual void show(void) = 0;
};
//*****************************
vehicle::vehicle(double speed = 80, int wheels = 4, double weight = 1000)
{
this->speed = speed;
this->weight = weight;
this->wheels = wheels;
}
报错信息:
2.cpp:14:73: error: default argument given for parameter 1 of ‘vehicle::vehicle(double, int, double)’ [-fpermissive]
vehicle::vehicle(double speed = 80, int wheels = 4, double weight = 1000)
^
2.cpp:10:5: note: previous specification in ‘vehicle::vehicle(double, int, double)’ here
vehicle(double speed = 80, int wheels = 4, double weight = 1000);
^~~~~~~
总结错误:
error:default argument given for parameter 和note: previous specification in……成对出现
改正后代码:
#include <iostream>
using namespace std;
class vehicle
{
protected:
double speed; //速度,公里/小时
int wheels; //轮子数
double weight; //重量
public:
vehicle(double speed = 80, int wheels = 4, double weight = 1000);
virtual void show(void) = 0;
};
//*****************************
vehicle::vehicle(double speed, int wheels, double weight)
{
this->speed = speed;
this->weight = weight;
this->wheels = wheels;
}
错误原因分析总结:
在类内函数的声明中可以声明默认参数值,也可以在类外函数的定义中声明默认参数值,但是不可以两个地方同时给定默认参数的值!!!!!!!