Как отладить "ISO C ++ запрещает объявление 'car' без типа"? - PullRequest
0 голосов
/ 28 ноября 2018

Я пытаюсь выяснить, почему я продолжаю получать эту ошибку на Dev C ++.Кажется, я не получаю никаких ошибок при использовании компьютеров из моей школы, поэтому мне было интересно, если я что-то здесь упустил.

[Ошибка] ISO C ++ запрещает объявление 'car' безтип -f разрешительный

код:

#include <iostream>
#include <string>
using namespace std;
class Car //class definition
{
private: int modelYr; string model; int speed;
public:
car(int year, string brand)
{ modelYr=year; model=brand; speed=0;} //Constructor
void accelerate() {speed = speed + 5;} //accelerate +5mph (mutator)
void brake() {speed = speed - 5;} //brake -5mph (mutator)
int get_modelYr() const {return modelYr;} //returns model year (accessor)
string get_model() const {return model;} //returns model name (accessor)
int getspeed() const {return speed;} //returns current speed (accessor)
};//end of class definition (note the semicolon ;)


int main() {
    Car vehicle; //create a vehicle from class Car
    string carModel;//car model to be input by user
    int carYear; //car year to be input by user
    const string line = "\n-------------------\n"; //used to display a line

  cout <<"Enter car model year (2000-current): ";
  while(! (cin >> carYear) || (carYear < 2000) ) //carYear validation
    { //checking for numeric year 2000 or later
         cin.clear();cin.ignore(10000,'\n');
         cout<<"sorry you must enter car year 2000 or later :"<<flush;
    } //while loop validation ends

  cout <<"Enter car model (example: GMC): "; cin >> carModel; 

  vehicle.car(carYear,carModel); 
 cout << line << "Starting speed for your " << vehicle.get_modelYr()
<< " " << vehicle.get_model() << " is " << vehicle.getspeed() << " mph" <<line;

for(int count=0;count<5;count++) //accelerate five times
{ vehicle.accelerate();
cout << "***ACCELERATING***\nSpeed is currently:"<< vehicle.getspeed() << endl;
}//end acceleration

           for ( int count=0;count<5 ;count++ )
           {vehicle.brake();
 cout << "***BRAKING***\nSpeed is currently: " << vehicle.getspeed() << endl;
           }

          cout << line << "Ending speed for your " << vehicle.get_modelYr()
 << " " << vehicle.get_model() << " is " << vehicle.getspeed() <<" mph"<< line;

return(0);
}//end of main program

1 Ответ

0 голосов
/ 28 ноября 2018

Ваш конструктор должен быть назван так же, как ваш класс.

#include <iostream>
#include <string>
using namespace std;
class Car //class definition
{
private: int modelYr; string model; int speed;
public:
Car(int year, string brand)
{ modelYr=year; model=brand; speed=0;} //Constructor
void accelerate() {speed = speed + 5;} //accelerate +5mph (mutator)
void brake() {speed = speed - 5;} //brake -5mph (mutator)
int get_modelYr() const {return modelYr;} //returns model year (accessor)
string get_model() const {return model;} //returns model name (accessor)
int getspeed() const {return speed;} //returns current speed (accessor)
};//end of class definition (note the semicolon ;)


int main() {
    Car vehicle; //create a vehicle from class Car
    string carModel;//car model to be input by user
    int carYear; //car year to be input by user
    const string line = "\n-------------------\n"; //used to display a line

  cout <<"Enter car model year (2000-current): ";
  while(! (cin >> carYear) || (carYear < 2000) ) //carYear validation
    { //checking for numeric year 2000 or later
         cin.clear();cin.ignore(10000,'\n');
         cout<<"sorry you must enter car year 2000 or later :"<<flush;
    } //while loop validation ends

  cout <<"Enter car model (example: GMC): "; cin >> carModel; 

  vehicle.car(carYear,carModel); 
 cout << line << "Starting speed for your " << vehicle.get_modelYr()
<< " " << vehicle.get_model() << " is " << vehicle.getspeed() << " mph" <<line;

for(int count=0;count<5;count++) //accelerate five times
{ vehicle.accelerate();
cout << "***ACCELERATING***\nSpeed is currently:"<< vehicle.getspeed() << endl;
}//end acceleration

           for ( int count=0;count<5 ;count++ )
           {vehicle.brake();
 cout << "***BRAKING***\nSpeed is currently: " << vehicle.getspeed() << endl;
           }

          cout << line << "Ending speed for your " << vehicle.get_modelYr()
 << " " << vehicle.get_model() << " is " << vehicle.getspeed() <<" mph"<< line;

return(0);
}//end of main program
...