Creating a Class

I have been learning C++ fairly well for the past few months in a course I'm taking, but I'm having a fair amount of difficulty creating a class. I have my first few accessor functions defined in a header, but I think that the trouble lies in the definition of the constructor I have.

1
2
3
4
5
Car::Car(int NewYearModel, string newMake)
{
     yearModel = newYearModel;
     newMake = make;
}


In my function main(), I have an instance of my car class, auto, with arguments passed through. Yet, I recieve the error of a missing semicolon when, in fact, all lines before it have semicolons.

I have tried my book, a friend, and numerous web resources and nothing seems to make sense. Any assistance would be greatly appreciated.

I think line 4 should be make = newMake;
I get some new errors with that change. Each of my "new" variables are coming out as undeclared. I can show what's there even with that new edit. I also get an "expected ; before } token" error at line 14, but all preceding lines have their proper semicolons.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "Car.h"

Car::Car()
{
     int newYearModel = 0;
     int newSpeed = 0;
     string newMake = "";
}

Car::Car(int NewYearModel, string newMake)
{
     yearModel = newYearModel;
     make = newMake
}

Car::~Car()
{
           
}

int Car::getYearModel() const
{
     return newYearModel;
}

string Car::getMake() const
{
     return newMake;
}

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

You are missing a ; at the end of line 13
Fixed it. I'm now working on my function, and using another webpage as a guide.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
    
}
    


It now says my declaration Car auto; does not declare anything. I'm also getting a bunch of "expected primary-expression before 'auto' " as well as semicolon errors.
Use something other than auto. this word is reserved or something(not sure about this). Make sure that you include "car.h" and all other required headers. You need a return statement too.
Use something other than auto. this word is reserved or something(not sure about this).


You are correct. auto is a reserved C++ keyword.
What's going on your default constructor?

1
2
3
4
5
6
Car::Car()
{
     int newYearModel = 0;
     int newSpeed = 0;
     string newMake = "";
}


You're declaring and initialising three local variables and then... doing nothing with them.

EDIT:

And then in your other constructor:

1
2
3
4
5
Car::Car(int NewYearModel, string newMake)
{
     yearModel = newYearModel;
     make = newMake
}


you're ignoring your first argument entirely.

Presumably yearModel, newYearModel and make are all class members, for this to compile? You haven't shown us your class definition, so I can't be sure.
Last edited on
Topic archived. No new replies allowed.