gpt4 book ai didi

c++ - C++ 中的类 : Declaring an Instance

转载 作者:行者123 更新时间:2023-11-30 01:56:03 25 4
gpt4 key购买 nike

我查阅了很多网站,但没有成功完成我的代码。

我正在尝试编写一些代码,在用户提供信息后吐出有关汽车的信息。编译后,编译器说“declaration does not declare anything”。这是代码:

 #include <iostream>
#include <string>
#include "Car.h"

using namespace std;

Car::Car()
{

}

void Car::accel()
{
speed += 5;
}

void Car::brake()
{
speed -= 5;
}

void Car::setSpeed(int newSpeed)
{
speed = newSpeed;
}

int Car::getSpeed()
{
return speed;
}

void Car::setMake(string newMake)
{
make = newMake;
}

string Car::getMake()
{
return make;
}

void Car::setYearModel(int newYearModel)
{
yearModel = newYearModel;
}

int Car::getYearModel()
{
return yearModel;
}

int main()
{
Car auto; //instance of class Car
int autoYear; //year of auto
string autoMake; //make of auto
int autoSpeed; //speed of auto

cout << "Enter the year model of your car. ";
cin >> autoYear;
cout << "Enter the make of your car. ";
cin >> autoMake;

auto.setYear(autoYear); //stores input year
auto.setMake(autoMake); //stores input make

}

还有标题,Car.h:

#include <iostream>
#include <string>

using namespace std;

#ifndef CAR_H
#define CAR_H

class Car
{
private:
int yearModel;
string make;
int speed;
public:
Car();
void accel();
void brake();
void setSpeed(int newSpeed);
int getSpeed();
void setMake(string newMake);
string getMake();
void setYearModel(int newYearModel);
int getYearModel();
};

#endif

每次我的对象 auto 出现时,我也会遇到缺少分号的错误(“expected ; before auto”和“expected primary-expression before auto”)。可能是什么问题?

最佳答案

auto 是关键字。您需要选择一个不同的名称。

关于c++ - C++ 中的类 : Declaring an Instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20180959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com