Constructor help

Updated code.

Error: Expected unqualified-id before '.' token line 72

 
Cars.DisplayCars(void);



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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>

using namespace std;

class Cars
{
public:
    void DisplayCars();
    Cars(); // this is the constructor
    Cars( int year, string manufacturer, string model, string type, int price, string vin, int warranty, int rebate);
    ~Cars(); //this is the deconstructor

private:
    struct details
    {
    int Year;
    string Manufacturer;
    string Model;
    string Type;
    int Price;
    string VIN;
    int Warranty;
    int Rebate;
    };
    details Car[2];
};
void Cars::DisplayCars(void)
{
    cout << Car[0].Year << "\t" << Car[1].Year;
    cout << Car[0].Manufacturer << "\t" << Car[1].Manufacturer;
    cout << Car[0].Model << "\t" << Car[1].Model;
    cout << Car[0].Type << "\t" << Car[1].Type;
    cout << Car[0].Price << "\t" << Car[1].Price;
    cout << Car[0].VIN << "\t" << Car[1].VIN;
    cout << Car[0].Warranty << "\t" << Car[1].Warranty;
    cout << Car[0].Rebate << "\t" << Car[1].Rebate;
    
}

Cars::~Cars(void)
{
    cout << "Freeing memory..." << endl;
}
Cars::Cars( int year, string manufacturer, string model, string type, int price, string vin, int warranty, int rebate)
{
    Car[0].Year = year;
    Car[0].Manufacturer = manufacturer;
    Car[0].Model = model;
    Car[0].Type = type;
    Car[0].Price = price;
    Car[0].VIN = vin;
    Car[0].Warranty = warranty;
    Car[0].Rebate = rebate;
}
Cars::Cars()
{
    cout << "Please enter the Year, Manufacturer, Model, Type, Price, VIN, Warranty and Rebate of the 2nd car." << endl;
    cout << "Year";
    cin >> Car[1].Year;
    cout << "Manufacturer";
    cin >> Car[1].Manufacturer;
    cout << "Model";
    cin >> Car[1].Model;
    cout << "Type";
    cin >> Car[1].Type;
    cout << "Price";
    cin >> Car[1].Price;
    cout << "VIN";
    cin >> Car[1].VIN;
    cout << "Warranty";
    cin >> Car[1].Warranty;
    cout << "Rebate";
    cin >> Car[1].Rebate;
}
int main ()
{

    Cars();
    Cars( 2013, "ford", "mustang", "GT", 27000, "asdfasdfasdf32df", 50000, 1000 );
    Cars.DisplayCars(void);

}
Last edited on
I would do it as follows

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Car
{
public:

    Car(); // this is the constructor
    // this is another constructor
    Car( int Year, string Manufacturer, string Model, string Type, int Price, string VIN, int Warranty, int Rebate );
    ~Car(); //this is the deconstructor

private:
    int m_Year;
    string m_Manufacturer;
    string m_Model;
    string m_Type;
    int m_Price;
    string m_VIN;
    int m_Warranty;
    int m_Rebate;
};

Car::Car()
{
  cout << "object is being created" << endl;

  cout << "Please enter the Year, Manufacturer, Model, Type, Price, VIN,   Warranty and Rebate of the 2nd car." << endl;
  cout << "Year";
  cin >> m_Year;
  cout << "Manufacturer";
  cin >> m_Manufacturer;
  cout << "Model";
  cin >> m_Model;
  cout << "Type";
  cin >> m_Type;
  cout << "Price";
  cin >> m_Price;
  cout << "VIN";
  cin >> m_VIN;
  cout << "Warranty";
  cin >> m_Warranty;
  cout << "Rebate";
  cin >> m_Rebate;
}

Car::Car( int Year, string Manufacturer, string Model, string Type, int Price, string VIN, int Warranty, int Rebate )
{
  m_Year         = Year;
  m_Manufacturer = Manufacturer;
  m_Model        = Model;
  m_Type         = Type;
  m_Price        = Price;
  m_VIN          = VIN;
  m_Warranty     = Warranty;
  m_Rebate       = Rebate;
}


Car::~Car(void)
{
  cout << "Freeing memory..." << endl;
}


int main()
{
  vector< Car * > cars;

  cars.push_back( new Car( 2013, "Ford", "Mustang", "Coupe", 27000, "WAUDFAFL9AN011586", 50000, 1000 ) );
  cars.push_back( new Car() );

  for( int i=0; i < cars.size(); i++ )
    delete cars[i];
}


I dont like using someone elses code. I appreciate it so i will use it to learn. After manually adding everything you suggested the compiler states

vector was not declared in the scope
expected primary expression before *
expected primary expression before >
cars was not declared

im guessing im missing aheader for the vector?

I have not gotten to vectors yet. I am unaware how to use them and if possible would rather not use them at this time. Was my original code that far off? Was only trying to make an array within my struct in the class. It seemed like a good idea :D
If you want to make your code work here are some suggestions.
I would get rid of the struct inside the class (and the array of 1 detail) and declare a global array of 2 Cars. SetCar1() is referencing Car[1] which doesn't exist.

Cars cars[2];

Also add () to the end of the 2 prototypes and move them outside of the class.
void SetCar0();
void SetCar1();

Then fixup your SetCar0/SetCar1 methods to use cars[0] or cars[1]

This should get you close.
Hi Michael,

I agree with binarybob's example. I would like to add one note, however. Even with simple code, if you are taking user input, it's good practice to add some error checking. For example, what if the user put in a negative number for the model year?

1
2
3
4
5
6
7
8
9
...
cout << "Year: " ;
cin >> m_Year ;
while( m_Year < 0 ) 
{
cout << "Please enter a valid model year: " ;
cin >> m_Year ;
}
...


Adding a few lines for error checking can keep garbage data out of your program.
Last edited on
Working on getting rid of the struct but i cant seem to figure out how to get the 2nd set to work. Keeps saying not declared in scope.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>

using namespace std;

class Cars
{
public:

    Cars(); // this is the constructor
    Cars( int year, string manufacturer, string model, string type, int price, string vin, int warranty, int rebate);
    void DisplayCars();
    ~Cars(); //this is the deconstructor

private:
    int Year;
    string Manufacturer;
    string Model;
    string Type;
    int Price;
    string VIN;
    int Warranty;
    int Rebate;

};

Cars::~Cars(void)
{
    cout << "Freeing memory..." << endl;
}
Cars::Cars( int year, string manufacturer, string model, string type, int price, string vin, int warranty, int rebate)
{
    Year = year;
    Manufacturer = manufacturer;
    Model = model;
    Type = type;
    Price = price;
    VIN = vin;
    Warranty = warranty;
    Rebate = rebate;
}
Cars::Cars()
{
    cout << "Please enter the Year, Manufacturer, Model, Type, Price, VIN, Warranty and Rebate of the 2nd car." << endl;
    cout << "Year";
    cin >> Year;
    cout << "Manufacturer";
    cin >> Manufacturer;
    cout << "Model";
    cin >> Model;
    cout << "Type";
    cin >> Type;
    cout << "Price";
    cin >> Price;
    cout << "VIN";
    cin >> VIN;
    cout << "Warranty";
    cin >> Warranty;
    cout << "Rebate";
    cin >> Rebate;
}
void Cars::DisplayCars(void)
{
    cout << Year << "\t" << year;
    cout << Manufacturer << "\t" << manufacturer;
    cout << Model << "\t" << model;
    cout << Type << "\t" << type;
    cout << Price << "\t" << price;
    cout << VIN << "\t" << vin;
    cout << Warranty << "\t" << warranty;
    cout << Rebate << "\t" << rebate;

}

int main ()
{
    Cars Car;
    Cars();
    Cars( 2013, "ford", "mustang", "GT", 27000, "asdfasdfasdf32df", 50000, 1000 );
    Cars.DisplayCars();




}


1
2
3
4
5
6
7
8
 cout << Year << "\t" << year;
    cout << Manufacturer << "\t" << manufacturer;
    cout << Model << "\t" << model;
    cout << Type << "\t" << type;
    cout << Price << "\t" << price;
    cout << VIN << "\t" << vin;
    cout << Warranty << "\t" << warranty;
    cout << Rebate << "\t" << rebate;


year, model, type, manufacturer, price etc not declared in scope?? declared in the other function....hmmm what am i doing wrong?
Last edited on
1
2
3
4
 Cars Car; //make a new Cars object called 'Car'
    Cars(); //make a new Cars object (temporary) and throw it away
    Cars( 2013, "ford", "mustang", "GT", 27000, "asdfasdfasdf32df", 50000, 1000 ); //ditto
    Cars.DisplayCars();


Line 4: You attempt to call a member function on something that is not an object of type 'Cars'; you must have an object of type 'Cars' to call it with.
Am i misunderstanding what an object is? DisplayCars is in class Cars.
Last edited on
Am i misunderstanding what an object is?

Yes. Cars is a class. Car (per Firedraco's post) is an instance of class Cars, aka an object.

1
2
Cars car1 (2013, "ford", "mustang", "GT", 27000, "asdfasdfasdf32df", 50000, 1000 );   // Create an instance of Cars
car1.DisplayCars();  // Display that instance 

Last edited on
Ok. I see whats going on. I really am mislableing and the use of 'Car' and 'Cars' is not a good labeling practice. Now that its calling the function correctly and i understand what i did wrong and im doing now....my error

1
2
3
4
5
6
7
8
 cout << Year << "\t" << year;
    cout << Manufacturer << "\t" << manufacturer;
    cout << Model << "\t" << model;
    cout << Type << "\t" << type;
    cout << Price << "\t" << price;
    cout << VIN << "\t" << vin;
    cout << Warranty << "\t" << warranty;
    cout << Rebate << "\t" << rebate;


is all thats left and it says year, manufacturer, model type.... all lower case are not declared. So im guessing i need to declare dual variables for 2 types of cars? That was the initial intention of the struct to keep from having

1
2
3
4
5
6
7
8
int year, Year;
string manufacturer, Manufacturer;
string model, Model;
string type, Type;
int price, Price;
string vin, VIN;
int warranty, Warranty;
int rebate, Rebate;


i appreciate you guys holding my hand, sorry about the mess.
You want a different car, declare a different instance. No need to declare additional variables.
1
2
Cars car2 (2010, "Chevy", "Corvette", "L6", 50000, "somevin", 50000, 0 );   // Create an instance of Cars
car2.DisplayCars();  // Display that instance  

Well that works but it wont display my material input from

 
Cars();

when this ^^^ runs, it asks me to input the information
 
Cars Car1(2010, "Chevy", "Corvette", "L6", 50000, "somevin", 50000, 0 );

when this ^^^^ runs it puts all the information into the variables under the Object Car1. But when i display using
 
Car1.DisplayCars(); 

it doesnt display any of the information the program has obtained from the user through Cars();


Last edited on
Since your default constructor prompts the users, then create an instance using the default constructor.
1
2
Cars car3;  // Invokes default constructor
car3.DisplayCars();  // Display that instance   


It's not a very good idea to prompt for input in a default constructor. At some point you might want to include Cars as a member of another class. The default constructor of Cars is going to get called when you instantiate the new class. You might be prompting for input when you're not exepcting it. It's better to have a separate function to prompt for input. Your default constuctor should establish default values for an instance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Cars::PromptForInput()
{  cout << "Please enter the Year, Manufacturer, Model, Type, Price, VIN, Warranty and Rebate." << endl;
    cout << "Year";
    cin >> Year;
    cout << "Manufacturer";
    cin >> Manufacturer;
    cout << "Model";
    cin >> Model;
    cout << "Type";
    cin >> Type;
    cout << "Price";
    cin >> Price;
    cout << "VIN";
    cin >> VIN;
    cout << "Warranty";
    cin >> Warranty;
    cout << "Rebate";
    cin >> Rebate;
}
Last edited on
I finally got the program complete.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

#include <iostream>
#include <string>
using namespace std;
class Cars
{
public:
    Cars(); // this is the constructor
    void InputCar1(); // this is the prototype to input car1
    void InputCar2( int y, string m, string mod, string t, int p, string v, int w, int r); 
 // this is prototype to request info for car 2
    void DisplayCars(); // this is the prototype to display results
    ~Cars(); //this is the deconstructor
private: // variables for only members of the class objects to access
    int year, Year;
    string manufacturer, Manufacturer;
    string model, Model;
    string type, Type;
    int price, Price;
    string vin, VIN;
    int warranty, Warranty;
    int rebate, Rebate;
};
Cars::Cars()
{
}
Cars::~Cars(void)
{
    cout << "Freeing memory..." << endl;
}
void Cars::InputCar2( int y, string m, string mod, string t, int p, string v, int w, int r)
{
    year = y;
    manufacturer = m;
    model = mod;
    type = t;
    price = p;
    vin = v;
    warranty = w;
    rebate = r;
}
void Cars::InputCar1()
{
    cout << "Please enter the Year, Manufacturer, Model, Type, Price, VIN,
                    Warranty and Rebate of the 2nd car." << endl;
    cout << "Year: ";
    cin >> Year;
    cout << "Manufacturer: ";
    cin >> Manufacturer;
    cout << "Model: ";
    cin >> Model;
    cout << "Type: ";
    cin >> Type;
    cout << "Price: ";
    cin >> Price;
    cout << "VIN: ";
    cin >> VIN;
    cout << "Warranty: ";
    cin >> Warranty;
    cout << "Rebate: ";
    cin >> Rebate;
}
void Cars::DisplayCars(void)
{
    cout << endl << "\t\tCar1\t\t\t Car2" << endl;
    cout << "Year:\t\t" << Year << "\t\t\t " << year << endl << endl;
    cout << "Manufacturer:\t" << Manufacturer << "\t\t\t " << manufacturer << endl << endl;
    cout << "Model:\t\t" << Model << "\t\t\t " << model << endl << endl;
    cout << "Type:\t\t" << Type << "\t\t\t " << type << endl << endl;
    cout << "Price:\t\t" << Price << "\t\t\t " << price << endl << endl;
    cout << "VIN:\t\t" << VIN << "\t " << vin << endl << endl;
    cout << "Warranty:\t" << Warranty << "\t\t\t " << warranty << endl << endl;
    cout << "Rebate:\t\t" << Rebate << "\t\t\t " << rebate << endl;
}
int main ()
{
    Cars Car; //Creates object
    Car.InputCar1(); // uses object to access variables for car 1
    Car.InputCar2(2010, "Chevy", "Corvette", "L6", 50000, "ASDFVVRVR4RFV43334", 50000, 0 );
 // uses object to access variables for car 2
 Car.DisplayCars(); // uses variables to display both cars
}
Topic archived. No new replies allowed.