我的要求:
定义一个名为里程表的类,用于跟踪汽车的燃油和里程。该类应具有成员变量来跟踪行驶里程和车辆的燃油效率(英里/加仑)。包括一个mutator函数,用于将里程表重置为零英里,一个用于设置燃油效率的mutator函数,一个mutator函数,它接受一个行程驱动的里程并将其添加到里程表的总数,以及一个返回加仑方法,返回加仑数自里程表上次重置以来车辆消耗的汽油。
将您的课程与测试程序一起使用,以创建具有不同燃油效率的多次行程。
#include <iostream>
using namespace std;
class Odometer
{
public:
Odometer();
void reset();
void totalfuel();
void input_miles(int getmiles);
void Odometer::set_fuel_efficiency(double fuel_efficiency);
double Odometer::getgallons();
int gallonsUsed;
private:
int milesDriven;
double fuel_efficiency;
int getmiles;
};
Odometer::Odometer()
{
milesDriven = 0;
fuel_efficiency = 0;
}
void Odometer::reset()
{
milesDriven = 0;
}
void Odometer::totalfuel()
{
fuel_efficiency = (milesDriven/gallonsUsed);
}
void Odometer::input_miles(int miles_driven)
{
milesDriven = milesDriven + miles_driven;
}
void Odometer::set_fuel_efficiency(double Fuel_efficiency)
{
fuel_efficiency = Fuel_efficiency;
}
double Odometer::getgallons()
{
return milesDriven/fuel_efficiency;
}
int main()
{
Odometer CarOdometer;
int number_of_miles_driven;
double fuel_efficiency;
cout << "Please enter the amount of miles driven : " << endl;
cin >> number_of_miles_driven;
CarOdometer.input_miles(number_of_miles_driven);
cout << "Enter Fuel Amount: " << endl;
cin >> fuel_efficiency;
return 0;
}
错误:
main.cpp:16:7: error: extra qualification �Odometer::� on member �set_fuel_efficiency� [-fpermissive]
void Odometer::set_fuel_efficiency(double fuel_efficiency);
^
main.cpp:18:9: error: extra qualification �Odometer::� on member �getgallons� [-fpermissive]
double Odometer::getgallons();
答案 0 :(得分:0)
您的成员函数声明具有封闭类的限定条件。根据C ++标准,这是无效的:
class Odometer {
void Odometer::member();
};
只需从成员声明中删除多余的资格:
class Odometer {
void member();
};
答案 1 :(得分:0)
只需更改
void Odometer::set_fuel_efficiency(double fuel_efficiency);
double Odometer::getgallons();
到
void set_fuel_efficiency(double fuel_efficiency);
double getgallons();