我按照你们告诉我的内容编辑了这个 . 它一直到这一行和错误
cout <<“起始速度是”<< honda.getSpeed()<< endl << endl
[错误]请求'honda'中的成员'getSpeed',这是非类型'Car(int,std :: string,int){aka Car(int,std :: basic_string,int)}'
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Car {
private:
int yearModel;
string make;
int speed;
public:
Car(int carYearModel, string carMake, int carSpeed = 0) {
yearModel = carYearModel;
make = carMake;
speed = carSpeed;
}
void accelerate() {
speed += 5;
}
void brake() {
speed -= 5;
}
int getSpeed() {
return speed;
}
int getyearModel() {
return yearModel;
}
string getMake() {
return make;
}
};
int main() {
int count, YearModel, speed, make;
string carMake;
cout << "What is the speed?";
cin >> speed;
cout << "What is the year of the model?";
cin >> YearModel;
cout << "What is the make?";
cin >> make;
Car honda(int carYearModel, string carMake, int carSpeed);
cout << "The starting speed is " << honda.getSpeed() << endl << endl;
cout << "The year and model is " << honda.getyearModel() << endl << endl;
cout << "The make of the car is " << honda.getMake() << endl << endl;
return 0;
}
2 回答
您的getter方法不应该要求用户输入 .
此外,它应该是汽车本田(carYearModel,carMake,carSpeed);并且您需要为变量carYearModel,carMake`和carSpeed赋值,然后才能将它们用作方法参数 . 您可以从用户那里获取这些值 . 调用方法时不包括参数数据类型 . 您只在定义方法时才这样做 .
声明一个名为
honda
的函数,它接受三个参数返回一个Car
. 你需要的是: