类(里程表) - 错误消息

时间:2012-12-06 22:16:29

标签: c++ debugging class

我想知道当我尝试编译代码时,是否有人能够告诉我这些错误消息的含义。

这是我得到的错误:
在函数'int main()'中: 与'std :: operator<< [with_Traits = std :: char_traits((std :: basic_ostr ...

)中的'operator<<'不匹配

它重复了一段时间。

我想发布我的完整代码,这样你就知道我的任务是什么,不是那么久! =)

#include <iostream>
#include <cstdlib>
using namespace std;

class Odometer

{
public:

Odometer();

void reset();
void totalfuel();

void input_miles(int getmiles);
void Odometer::set_fuel_efficiency(double fuel_efficiency);

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;
} 

// ======================
// main function
// ======================
int main()
{
// Two test trips
Odometer trip1, trip2;

trip1.reset();
trip1.set_fuel_efficiency(45);
trip1.input_miles(100);
cout << "For your fuel-efficient small car:" << endl;
cout << "After 100 miles, " << trip1.totalfuel() << " gallons used." << endl;
trip1.input_miles(50);
cout << "After another 50 miles, " << trip1.totalfuel() << " gallons used." << endl;

trip2.reset();
trip2.set_fuel_efficiency(13);
trip2.input_miles(100);
cout << "For your gas guzzler:" << endl;
cout << "After 100 miles, " << trip2.totalfuel() << " gallons used." << endl;
trip2.input_miles(50);
cout << "After another 50 miles, " << trip2.totalfuel() << " gallons used." << endl;

system("PAUSE");
return 0;
}

2 个答案:

答案 0 :(得分:4)

您希望cout << void打印什么?

totalfuel()返回void,您将其作为参数传递给cout::operator <<。你的意思是从方法中返回一些内容吗?

也许:

double Odometer::totalfuel()
{
    fuel_efficiency = (milesDriven/gallonsUsed);
    return fuel_efficiency;
}

答案 1 :(得分:3)

totalFuel()返回void。我认为你的意思是调用getgallons()方法。