C ++中的类:声明实例

时间:2013-11-24 21:02:25

标签: c++ class

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

我试图写一些代码,一旦用户提供信息,就会吐出有关汽车的信息。编译后,编译器说“声明没有声明任何内容”。这是代码:

 #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之前”和“在auto之前预期的primary-expression”),我也会因缺少分号而出错。可能是什么问题?

1 个答案:

答案 0 :(得分:4)

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