I don't know what I am doing wrong here.

I get bunch of errors when I compile.
I have three files Car.h, Car.cpp, automobile.cpp. Automobile.cpp has the main(). I'm using visual studio 2012 Professional.

error list:
>c:\users\sang\documents\visual studio 2012\projects\car\car\car.h(56): error C2864: 'Car::Speed' : only static const integral data members can be initialized within a class
1>c:\users\sang\documents\visual studio 2012\projects\car\car\automobile.cpp(78): error C3867: 'Car::Accelerate': function call missing argument list; use '&Car::Accelerate' to create a pointer to member
1>c:\users\sang\documents\visual studio 2012\projects\car\car\automobile.cpp(82): error C3867: 'Car::Brake': function call missing argument list; use '&Car::Brake' to create a pointer to member
1>c:\users\sang\documents\visual studio 2012\projects\car\car\automobile.cpp(96): fatal error C1075: end of file found before the left brace '{' at 'c:\users\sang\documents\visual studio 2012\projects\car\car\automobile.cpp(49)' was matched
1> Car.cpp
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(29): warning C4627: '#include "Car.h"': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(37): error C2653: 'Car' : is not a class or namespace name
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(38): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(40): error C2065: 'Speed' : undeclared identifier
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(41): warning C4508: 'Car' : function should return a value; 'void' return type assumed
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(45): error C2653: 'Car' : is not a class or namespace name
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(45): error C2146: syntax error : missing ';' before identifier 'getMake'
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(45): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(46): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(48): error C2065: 'Make' : undeclared identifier
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(52): error C2653: 'Car' : is not a class or namespace name
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(54): error C2065: 'YearModel' : undeclared identifier
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(58): error C2653: 'Car' : is not a class or namespace name
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(60): error C2065: 'Speed' : undeclared identifier
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(65): error C2653: 'Car' : is not a class or namespace name
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(67): error C2065: 'Speed' : undeclared identifier
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(70): error C2653: 'Car' : is not a class or namespace name
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(72): error C2065: 'Speed' : undeclared identifier
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(77): error C2065: 'cout' : undeclared identifier
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(78): error C2065: 'cout' : undeclared identifier
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(79): error C2065: 'cout' : undeclared identifier
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(80): error C2065: 'cout' : undeclared identifier
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(81): error C2065: 'cout' : undeclared identifier
1>c:\users\sang\documents\visual studio 2012\projects\car\car\car.cpp(82): error C2065: 'cout' : undeclared identifier





below is Car.h
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
#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

 class Car
{
	// Public Interface

private:
	 

	//  Variable Declarations
	int YearModel, Speed=0;
	std::string Make;


public:
	// Class Constructor(s)
	Car( );

	//Client Methods
	string getMake();
    int getModel();
    int getSpeed();
    void Accelerate();
 	void Brake();
 	void displayMenu();

	// Class Destructor
	~Car(void);


};


  


below is Car.cpp
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
#include "Car.h"  // Class Definition file
#include <stdio.h>
#include "stdafx.h"
#include <string.h>


// Class Default Constructor

 Car::Car( )
{
    
   Speed = 0;
 }


 //To get who makes the car. 
string Car::getMake()
{
 
 return Make;
 }

 //To get the year of the car.
 int Car::getModel()
 {
 return YearModel;
 }

 //To holds the car actual speed.
 int Car::getSpeed()
 {
 	return Speed;
 }

 //To increase speed by 5.
 
 void Car::Accelerate()
 {
 	Speed = Speed + 5;
 }
 //To drop the speed of the car by 5.
 void Car::Brake() 
 {
 	Speed = Speed - 5;
 }

void displayMenu()
{
	cout <<"\n				Menu\n";
	cout << "----------------------------\n";
    cout << "A)Accelerate the Car\n";
	cout << "B)Push the Brake on the Car\n";
	cout << "C)Exit the program\n\n";
    cout << "Enter your choice: ";
}



below is automobile.cpp
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
#include "stdafx.h"  // Defines IDE required external definition files
#include <iostream>  // Defines objects and classes used for stream I/O
// Class Definition header file(s)
#include "Car.h"
#include <string.h>

// Namespaces utilized in this program
using namespace std; // Announces to the compiler that members of the namespace "std"
                     // are utilized in this program

int main()          
{
	  
 
 char selection; //Menu selection
  
 Car CCar;

 
 
// cout << "The speed of the SUV is set to: " << Speed <<endl;
// Car first( 2007, "GMC", Speed);
 
 	//Display the menu and get a valid selection
 	do
 	{
 		CCar.displayMenu();
 		cin >> selection;
 		while(toupper(selection) < 'A' || toupper(selection) > 'C')
 		{
 			cout << "Please make a choice of A or B or C:";
 			cin >> selection;
 		}
 
 	//Process the user's menu selection
 		{
 			switch (selection)
 			{
 			case 'a':
 			case 'A': cout << "Acceleration Selected ";
 				cout << CCar.Accelerate << endl;
 			break;
 			case 'b':
 		case 'B': cout << "Brake Selected";
 			cout << CCar.Brake  << endl;
 			break;
 			}
 		}while (toupper(selection) != 'C');

	 
	return 0;
}

Remove the =0 in line 15 of Car.h. This is only valid for static/class variables.
Last edited on
car.h line 15 - you can't initialize a variable inside of a class that's not a const static integral type (Speed).

automobile.cpp line 41 - you need to call the function CCar.Accelerate() with parentheses, same for line 45.

automobile.cpp line 36 - you have an un-paired bracket, which throws off all brackets following it.

Finally, the fact that your project uses pre-compiled headers seems to throw off the inclusion of the car header in car.cpp, which means it won't recognize the Car class. You'd need to start a new project, or look online for that particular warning.
Anyone else. I corrected those but still not successful. I'm lost.
What errors are you receiving now?
Topic archived. No new replies allowed.