自己拿示例代码改的
//构造函数、析构函数、和range()都在类的内部定义(内联函数,inline)
//将类的三个字段全部设置为私有变量
//添加存取函数用于获得三个私有变量的值
#include <iostream>
using namespace std;
//定义Vehicle类
class Vehicle {
int passengers; // number of passengers
int fuelcap; // fuel capacity in gallons
int mpg; // fuel consumption in miles per gallon
public:
// This is a constructor for Vehicle.
Vehicle(int p, int f, int m) {
passengers = p;
fuelcap = f;
mpg = m;
}
// Compute and return the range.
int range() { return mpg * fuelcap; }
// Accessor functions.
int get_passengers() { return passengers; }
int get_fuelcap() { return fuelcap; }
int get_mpg() { return mpg; }
};
int main() {
// Pass values to Vehicle constructor.
Vehicle minivan(7, 16, 21);
Vehicle sportscar(2, 14, 12);
if(&minivan.get_passengers*2) //这一行是我修改了的部分
printf("1");
return 0;
}
会报错error: invalid operands of types 'int (Vehicle::*)()' and 'int' to binary 'operator*'。
他们不都是int吗,为什么不能计算