Sean Roberson
Sean Roberson

Reputation: 142

Classes in C++: Declaring an Instance

I have consulted many websites but have not successfully completed my code.

I am trying to write some code that spits out information about a car, once the user provides information. After compiling, the compiler says that "declaration does not declare anything." Here's the code:

 #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

}

And the header, 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

I also have errors for missing semicolons every time my object auto appears ("expected ; before auto" and "expected primary-expression before auto"). What could be the problem?

Upvotes: 1

Views: 107

Answers (1)

Alan Stokes
Alan Stokes

Reputation: 18964

auto is a keyword. You'll need to pick a different name.

Upvotes: 4

Related Questions