Class compile-time error "C++ forbids declaration of Class with no type"

I'm working on a C++ program in which we're learning to properly use a Class. In my program I'm supposed to have a Class named "Car" which has a function called accelerate be called 5 times and then a function called brake be called 5 times. I've deleted and added things so much that now the compiler seems to not even know where to start on how much I've messed up. I've looked on multiple forums but my compiler is too vague at the moment for me to find any help. Here are my errors.

lab12a.cpp:18: error: ISO C++ forbids declaration of âClassâ with no type
lab12a.cpp:19: error: ISO C++ forbids declaration of âClassâ with no type
lab12a.cpp: In function âint main()â:
lab12a.cpp:55: error: expected primary-expression before â<<â token
lab12a.cpp:57: error: expected primary-expression before â<<â token
lab12a.cpp:57: error: âgetSpeedâ was not declared in this scope
lab12a.cpp:59: error: âAccelerateâ was not declared in this scope
lab12a.cpp:69: error: âBrakeâ was not declared in this scope

And here is the code

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
// 2-1-2018
// Lab12A
// Uses a class named Car to perform accelarate and brake functions

#include <iostream>
#include <string>

using namespace std;

class Car
{
   private:
   int Year,Speed;
   string Make;
   
   public:
   Class();
   Class(int& Year,int& Speed,string& Make);

   int getSpeed()
   {
      return Speed;
   }
   int getYear()
   {
      return Year;
   }
   string getMake()
   {
      return Make;
   }    
   int Accelerate()
   {
      Speed = Speed + 5;
      return Speed;
   }
   int Brake()
   {
      Speed = Speed - 5;
      return Speed;
   }  
      
};

int main()
{
   Car ClassCar;
   int Year;
   int Speed;
   string Make;
   
   cout << "Please enter the year of your car: ";
   cin >> Year;
   cout << endl; << "Please enter the make of your car: ";
   cin >> Make;
   cout << endl; << "Your car is currently going " << getSpeed() 
        << "mph. It will now accelerate." << endl; 
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will now start to brake." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " End of program." << endl;
   return 0;   
}   
   
   
Last edited on
public:
Class();
Class(int& Year,int& Speed,string& Make);

this should be Car. you don't have class Class() defined, so it is confused.
That was a silly mistake on my part, been working on hours and just forgot about that! I fixed it and now there are 2 errors gone. This is what's left.

lab12a.cpp: In function âint main()â:
lab12a.cpp:55: error: expected primary-expression before â<<â token
lab12a.cpp:57: error: expected primary-expression before â<<â token
lab12a.cpp:57: error: âgetSpeedâ was not declared in this scope
lab12a.cpp:59: error: âAccelerateâ was not declared in this scope
lab12a.cpp:69: error: âBrakeâ was not declared in this scope

And here's my code

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

// 2-1-2018
// Lab12A
// Uses a class named Car to perform accelarate and brake functions

#include <iostream>
#include <string>

using namespace std;

class Car
{
   private:
   int Year,Speed;
   string Make;
   
   public:
   Car();
   Car(int& Year,int& Speed,string& Make);

   int getSpeed()
   {
      return Speed;
   }
   int getYear()
   {
      return Year;
   }
   string getMake()
   {
      return Make;
   }    
   int Accelerate()
   {
      Speed = Speed + 5;
      return Speed;
   }
   int Brake()
   {
      Speed = Speed - 5;
      return Speed;
   }  
      
};

int main()
{
   Car ClassCar;
   int Year;
   int Speed;
   string Make;
   
   cout << "Please enter the year of your car: ";
   cin >> Year;
   cout << endl; << "Please enter the make of your car: ";
   cin >> Make;
   cout << endl; << "Your car is currently going " << getSpeed() 
        << "mph. It will now accelerate." << endl; 
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will now start to brake." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " End of program." << endl;
   return 0;   
}   

Last edited on
you don't use ; to chain prints.

just say
cout << "words and stuff" << variable << endl; // one ; at the end only.

<< blah is confusing the compiler, it cant tell what you want, its probably trying to do a bit shift (another use of the << operator) on something unsupported here.

I see what you meant and I removed just 1 semicolon from an endline and got rid of 2 errors. Now all that's left is:
lab12a.cpp: In function âint main()â:
lab12a.cpp:57: error: âgetSpeedâ was not declared in this scope
lab12a.cpp:59: error: âAccelerateâ was not declared in this scope
lab12a.cpp:69: error: âBrakeâ was not declared in this scope

I'm guessing something is wrong with my initialization in int main. I might have messed up something fundamental with the constructors for the class though.. honestly not sure. Here's the revised code:

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

// 2-1-2018
// Lab12A
// Uses a class named Car to perform accelarate and brake functions

#include <iostream>
#include <string>

using namespace std;

class Car
{
   private:
   int Year,Speed;
   string Make;
   
   public:
   Car();
   Car(int& Year,int& Speed,string& Make);

   int getSpeed()
   {
      return Speed;
   }
   int getYear()
   {
      return Year;
   }
   string getMake()
   {
      return Make;
   }    
   int Accelerate()
   {
      Speed = Speed + 5;
      return Speed;
   }
   int Brake()
   {
      Speed = Speed - 5;
      return Speed;
   }  
      
};

int main()
{
   Car ClassCar;
   int Year;
   int Speed;
   string Make;
   
   cout << "Please enter the year of your car: ";
   cin >> Year;
   cout << endl << "Please enter the make of your car: ";
   cin >> Make;
   cout << endl << "Your car is currently going " << getSpeed() 
        << "mph. It will now accelerate." << endl; 
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << Accelerate() << "mph."
        << " Your car will now start to brake." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << Brake() << "mph."
        << " End of program." << endl;
   return 0;   
}   



Last edited on
You're calling Car::getSpeed(), Car::Accelerate() and Car::Brake() as if they were free functions, rather than methods on your ClassCar object.

Also, nowhere in your code do you do anything with the Year and Make values that the user inputs. You're certainly not passing them into your ClassCar object.
I think I got it a little bit further, but I still don't really understand what you mean. I asked my teacher in lab today about it and got a little more of it understood, but there must still be something i'm missing. Now I have many more errors but their all variations of the same problem, so I think it's just with my variables.

lab12a.cpp: In member function âint Car::getSpeed()â:
lab12a.cpp:27: error: âSpeedâ was not declared in this scope
lab12a.cpp: In member function âint Car::getYear()â:
lab12a.cpp:31: error: âYearâ was not declared in this scope
lab12a.cpp: In member function âstd::string Car::getMake()â:
lab12a.cpp:35: error: âMakeâ was not declared in this scope
lab12a.cpp: In member function âint Car::Accelerate()â:
lab12a.cpp:39: error: âSpeedâ was not declared in this scope
lab12a.cpp: In member function âint Car::Brake()â:
lab12a.cpp:44: error: âSpeedâ was not declared in this scope
lab12a.cpp: In function âint main()â:
lab12a.cpp:64: error: request for member âgetSpeedâ in âClassCarâ, which is of non-class type âCar(int, int, std::string)â
lab12a.cpp:66: error: request for member âAccelerateâ in âClassCarâ, which is of non-class type âCar(int, int, std::string)â
lab12a.cpp:68: error: request for member âAccelerateâ in âClassCarâ, which is of non-class type âCar(int, int, std::string)â
lab12a.cpp:70: error: request for member âAccelerateâ in âClassCarâ, which is of non-class type âCar(int, int, std::string)â
lab12a.cpp:72: error: request for member âAccelerateâ in âClassCarâ, which is of non-class type âCar(int, int, std::string)â
lab12a.cpp:74: error: request for member âAccelerateâ in âClassCarâ, which is of non-class type âCar(int, int, std::string)â
lab12a.cpp:76: error: request for member âBrakeâ in âClassCarâ, which is of non-class type âCar(int, int, std::string)â
lab12a.cpp:78: error: request for member âBrakeâ in âClassCarâ, which is of non-class type âCar(int, int, std::string)â
lab12a.cpp:80: error: request for member âBrakeâ in âClassCarâ, which is of non-class type âCar(int, int, std::string)â
lab12a.cpp:82: error: request for member âBrakeâ in âClassCarâ, which is of non-class type âCar(int, int, std::string)â
lab12a.cpp:84: error: request for member âBrakeâ in âClassCarâ, which is of non-class type âCar(int, int, std::string)â

Here's the code now:
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
85
86
// 2-1-2018
// Lab12A
// Uses a class named Car to perform accelarate and brake functions

#include <iostream>
#include <string>

using namespace std;

class Car
{
   private:
   int year,speed;
   string make;
   
   public:
   Car(int year,int speed,string make)
   {
      int Year = year;
      int Speed = speed;
      string Make = make;
   }

   int getSpeed()
   {
      return Speed;
   }
   int getYear()
   {
      return Year;
   }
   string getMake()
   {
      return Make;
   }    
   int Accelerate()
   {
      Speed = Speed + 5;
      return Speed;
   }
   int Brake()
   {
      Speed = Speed - 5;
      return Speed;
   }  
      
};

int main()
{
   
   int Year;
   int Speed;
   string Make;
   
   cout << "Please enter the year of your car: ";
   cin >> Year;
   cout << endl << "Please enter the make of your car: ";
   cin >> Make;
   
   Car ClassCar(int year,int speed,string make);
   
   cout << endl << "Your car is currently going " << ClassCar.getSpeed() 
        << "mph. It will now accelerate." << endl; 
   cout << "Your car is now going " << ClassCar.Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << ClassCar.Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << ClassCar.Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << ClassCar.Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << ClassCar.Accelerate() << "mph."
        << " Your car will now start to brake." << endl;
   cout << "Your car is now going " << ClassCar.Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << ClassCar.Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << ClassCar.Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << ClassCar.Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << ClassCar.Brake() << "mph."
        << " End of program." << endl;
   return 0;   
}   

@Toasthat,
EDIT: Wrapping acceleration / breaking in a while loop . Commented

Your program should work well but you are missing some stuff.
You fixed the first point that @MikeyBoy pointed out but you did not fix the second.

First take a look at your class again.

My changes are highlighted in bold

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 2-1-2018
// Lab12A
// Uses a class named Car to perform accelarate and brake functions

#include <iostream>
#include <string>

using namespace std;

class Car
{
   private:
   int year,speed;
   string make;
   
   public:
   Car(int year,int speed,string make)
   {
      this->year = year;
      this->speed = speed;
      this->make = make;
   }

// if this-> confuses you, use this constructor instead, 
/*Car(int yr, int sp, string mk)
{
    year = yr; 
    speed = sp;
    make = mk;
}*/

   int getSpeed()
   {
      //return Speed; 
    //Speed is not defined. Look at your class[Line 13, int year, speed[!=Speed]
      return speed;
   }
   int getYear()
   {
      return year;
   }
   string getMake()
   {
      return make;
   }    
   int Accelerate()
   {
      speed = speed + 5;
      return speed;
     //return speed += 5; 
   }
   int Brake()
   {
      speed = speed - 5;
      return speed;
     // return speed -= 5;
   }  
      
};

int main()
{
   
   int Year = 0; // make it a habit to initialize your variables
  //int Speed; never used
   string Make;
   
   cout << "Please enter the year of your car: ";
   cin >> Year;
   cout << endl << "Please enter the make of your car: ";
   cin >> Make;
   
   //Car ClassCar(int year,int speed,string make)
   Car ClassCar(Year, 0, Make); // speed is 0
   
  // use your car make
  string carMake = ClassCar.getMake();

   cout << endl << "Your " << carMake << " is currently going " << ClassCar.getSpeed() 
        << "mph. It will now accelerate." << endl; 
   cout << "Your " << carMake << " is now going " << ClassCar.Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << ClassCar.Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << ClassCar.Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << ClassCar.Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   cout << "Your car is now going " << ClassCar.Accelerate() << "mph."
        << " Your car will now start to brake." << endl;
   cout << "Your car is now going " << ClassCar.Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << ClassCar.Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << ClassCar.Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << ClassCar.Brake() << "mph."
        << " Your car will brake again." << endl;
   cout << "Your car is now going " << ClassCar.Brake() << "mph."
        << " End of program." << endl;


/*
 // a little cleaner repetition of accelerate/break
// use your car make and year
   string car = ClassCar.getMake() + "["+ to_string(ClassCar.getYear())+ "]";

   cout << endl << "Your " << car << " is currently going " << ClassCar.getSpeed() 
        << "mph. It will now accelerate." << endl; 
    
    int maxSpeed = 50;
    
    while(ClassCar.getSpeed() <maxSpeed)
    {
        cout << "Your " << car << " is now going " << ClassCar.Accelerate() << "mph."
        << " Your car will Accelerate again." << endl;
   }
   
   cout << "Breaking the car. Fasten your seat belt..." << endl;
   
   while(ClassCar.getSpeed() >0)
   {
    cout << "Your " << car << " is now going " << ClassCar.Brake() << "mph."
        << " Your car will brake again." << endl;
   }
   cout << " End of program." << endl;
  
}  
*/ 
   return 0; 
}


Hope this helps you gain better understanding. Good luck.
Last edited on
Topic archived. No new replies allowed.